input: keyboard: rk_keys: add rk_keys.h
[firefly-linux-kernel-4.4.55.git] / drivers / input / misc / ricoh619-pwrkey.c
1 /*
2 * driver/input/misc/ricoh619-pwrkey.c
3 *
4 * Power Key driver for RICOH RC5T619 power management chip.
5 *
6 * Copyright (C) 2012-2013 RICOH COMPANY,LTD
7 *
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/errno.h>
27 #include <linux/input.h>
28 #include <linux/interrupt.h>
29 #include <linux/platform_device.h>
30 #include <linux/mfd/ricoh619.h>
31 #include <linux/spinlock.h>
32 #include <linux/timer.h>
33 #include <linux/pm.h>
34 #include <linux/slab.h>
35 #include <linux/pm_runtime.h>
36 #include <linux/workqueue.h>
37 #include <linux/gpio.h>
38 #include <linux/of.h>
39 #include <linux/delay.h>
40
41 #include <linux/mfd/ricoh619.h>
42 #include <linux/irq.h>
43 #include <linux/irqdomain.h>
44
45 #define RICOH619_ONKEY_TRIGGER_LEVEL    0
46 #define RICOH619_ONKEY_OFF_IRQ          0
47
48 struct ricoh619_pwrkey {
49         struct device *dev;
50         struct input_dev *pwr;
51         #if RICOH619_ONKEY_TRIGGER_LEVEL
52                 struct timer_list timer;
53         #endif
54         struct workqueue_struct *workqueue;
55         struct work_struct work;
56         unsigned long delay;
57         int key_irq;
58         bool pressed_first;
59         struct ricoh619_pwrkey_platform_data *pdata;
60         spinlock_t lock;
61 };
62
63 struct ricoh619_pwrkey *g_pwrkey;
64
65 #if RICOH619_ONKEY_TRIGGER_LEVEL
66 void ricoh619_pwrkey_timer(unsigned long t)
67 {
68         queue_work(g_pwrkey->workqueue, &g_pwrkey->work);
69 }
70 #endif
71 extern u8 ricoh619_pwr_key_reg;
72 static void ricoh619_irq_work(struct work_struct *work)
73 {
74         /* unsigned long flags; */
75         uint8_t val;
76         int i=0;
77
78 //      printk("PMU: %s: \n",__func__);
79         //spin_lock_irqsave(&g_pwrkey->lock, flags);
80         if((ricoh619_pwr_key_reg & 0x01) && ricoh619_pwrkey_wakeup){
81                 printk("PMU: %s: pwrkey_wakeup\n",__func__);
82                 ricoh619_pwrkey_wakeup = 0;
83                 input_event(g_pwrkey->pwr, EV_KEY, KEY_POWER, 1);
84                 input_event(g_pwrkey->pwr, EV_SYN, 0, 0);
85                 input_event(g_pwrkey->pwr, EV_KEY, KEY_POWER, 0);
86                 input_event(g_pwrkey->pwr, EV_SYN, 0, 0);
87                 do{
88                         ricoh619_read(g_pwrkey->dev->parent, RICOH619_INT_MON_SYS, &val);
89                 val &= 0x01;
90                         i += 1;
91                         msleep(100);
92                 }while(val && (i < 15));
93                 return;
94         }
95         ricoh619_read(g_pwrkey->dev->parent, RICOH619_INT_MON_SYS, &val);
96         dev_dbg(g_pwrkey->dev, "pwrkey is pressed?(0x%x): 0x%x\n",
97                                                 RICOH619_INT_MON_SYS, val);
98 //      printk(KERN_INFO "PMU: %s: val=0x%x\n", __func__, val);
99         val &= 0x1;
100         if(val){
101                 #if (RICOH619_ONKEY_TRIGGER_LEVEL)
102                 g_pwrkey->timer.expires = jiffies + g_pwrkey->delay;
103                 add_timer(&g_pwrkey->timer);
104                 #endif
105                 if (!g_pwrkey->pressed_first){
106                         g_pwrkey->pressed_first = true;
107 //                      printk("PMU1: %s: Power Key!!!\n",__func__);
108                         //input_report_key(g_pwrkey->pwr, KEY_POWER, 1);
109                         //input_sync(g_pwrkey->pwr);
110                         input_event(g_pwrkey->pwr, EV_KEY, KEY_POWER, 1);
111                         input_event(g_pwrkey->pwr, EV_SYN, 0, 0);
112                 }
113         } else {
114                 if (g_pwrkey->pressed_first) {
115 //                      printk(KERN_INFO "PMU2: %s: Power Key!!!\n", __func__);
116                         /* input_report_key(g_pwrkey->pwr, KEY_POWER, 0); */
117                         /* input_sync(g_pwrkey->pwr); */
118                         input_event(g_pwrkey->pwr, EV_KEY, KEY_POWER, 0);
119                         input_event(g_pwrkey->pwr, EV_SYN, 0, 0);
120                 }
121                 g_pwrkey->pressed_first = false;
122         }
123
124         /* spin_unlock_irqrestore(&g_pwrkey->lock, flags); */
125 }
126
127 static irqreturn_t pwrkey_irq(int irq, void *_pwrkey)
128 {
129 //      printk(KERN_INFO "PMU: %s:\n", __func__);
130 //      rk_send_wakeup_key();
131         #if (RICOH619_ONKEY_TRIGGER_LEVEL)
132         g_pwrkey->timer.expires = jiffies + g_pwrkey->delay;
133         add_timer(&g_pwrkey->timer);
134         #else
135         queue_work(g_pwrkey->workqueue, &g_pwrkey->work);
136         #endif
137         return IRQ_HANDLED;
138 }
139
140 #if RICOH619_ONKEY_OFF_IRQ
141 static irqreturn_t pwrkey_irq_off(int irq, void *_pwrkey)
142 {
143         dev_warn(g_pwrkey->dev, "ONKEY is pressed long time!\n");
144         return IRQ_HANDLED;
145 }
146 #endif
147
148 #ifdef CONFIG_OF
149 static struct ricoh619_pwrkey_platform_data *
150 ricoh619_pwrkey_dt_init(struct platform_device *pdev)
151 {
152         struct device_node *nproot = pdev->dev.parent->of_node;
153         struct device_node *np;
154         struct ricoh619_pwrkey_platform_data *pdata;
155
156         if (!nproot)
157                 return pdev->dev.platform_data;
158
159         np = of_find_node_by_name(nproot, "pwrkey");
160         if (!np) {
161                 dev_err(&pdev->dev, "failed to find pwrkey node\n");
162                 return NULL;
163         }
164
165         pdata = devm_kzalloc(&pdev->dev,
166                         sizeof(struct ricoh619_pwrkey_platform_data),
167                         GFP_KERNEL);
168
169         of_property_read_u32(np, "ricoh,pwrkey-delay-ms", &pdata->delay_ms);
170         of_node_put(np);
171
172         return pdata;
173 }
174 #else
175 static struct ricoh619_pwrkey_platform_data *
176 ricoh619_pwrkey_dt_init(struct platform_device *pdev)
177 {
178         return pdev->dev.platform_data;
179 }
180 #endif
181
182 static int ricoh619_pwrkey_probe(struct platform_device *pdev)
183 {
184         struct input_dev *pwr;
185         int key_irq;
186         int err;
187         struct ricoh619_pwrkey *pwrkey;
188         struct ricoh619_pwrkey_platform_data *pdata;
189         struct ricoh619 *ricoh619 = dev_get_drvdata(pdev->dev.parent);
190         uint8_t val;
191
192 //      printk("PMU: %s: \n",__func__);
193
194         pdata = ricoh619_pwrkey_dt_init(pdev);
195         if (!pdata) {
196                 dev_err(&pdev->dev, "platform data isn't assigned to "
197                         "power key\n");
198                 return -EINVAL;
199         }
200         key_irq  = irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_POWER_ON);
201         printk(KERN_INFO "PMU1: %s: key_irq=%d\n", __func__, key_irq);
202         pwrkey = kzalloc(sizeof(*pwrkey), GFP_KERNEL);
203         if (!pwrkey)
204                 return -ENOMEM;
205
206         pwrkey->dev = &pdev->dev;
207         pwrkey->pdata = pdata;
208         pwrkey->pressed_first = false;
209         pwrkey->delay = HZ / 1000 * pdata->delay_ms;
210         g_pwrkey = pwrkey;
211         pwr = input_allocate_device();
212         if (!pwr) {
213                 dev_dbg(&pdev->dev, "Can't allocate power button\n");
214                 err = -ENOMEM;
215                 goto free_pwrkey;
216         }
217         input_set_capability(pwr, EV_KEY, KEY_POWER);
218         pwr->name = "ricoh619_pwrkey";
219         pwr->phys = "ricoh619_pwrkey/input0";
220         pwr->dev.parent = &pdev->dev;
221
222         #if RICOH619_ONKEY_TRIGGER_LEVEL
223         init_timer(&pwrkey->timer);
224         pwrkey->timer.function = ricoh619_pwrkey_timer;
225         #endif
226
227         spin_lock_init(&pwrkey->lock);
228         err = input_register_device(pwr);
229         if (err) {
230                 dev_dbg(&pdev->dev, "Can't register power key: %d\n", err);
231                 goto free_input_dev;
232         }
233         pwrkey->key_irq = key_irq;
234         pwrkey->pwr = pwr;
235         platform_set_drvdata(pdev, pwrkey);
236
237         /* Check if power-key is pressed at boot up */
238         err = ricoh619_read(pwrkey->dev->parent, RICOH619_INT_MON_SYS, &val);
239         if (err < 0) {
240                 dev_err(&pdev->dev, "Key-press status at boot failed rc=%d\n",
241                                                                          err);
242                 goto unreg_input_dev;
243         }
244         val &= 0x1;
245         if (val) {
246                 input_report_key(pwrkey->pwr, KEY_POWER, 1);
247 //              printk(KERN_INFO "******KEY_POWER:1\n");
248                 input_sync(pwrkey->pwr);
249                 pwrkey->pressed_first = true;
250         }
251
252         #if !(RICOH619_ONKEY_TRIGGER_LEVEL)
253                 /* trigger both edge */
254                 ricoh619_set_bits(pwrkey->dev->parent, RICOH619_PWR_IRSEL, 0x1);
255         #endif
256         err = request_threaded_irq(key_irq, NULL, pwrkey_irq,IRQF_ONESHOT, "ricoh619_pwrkey", pwrkey);
257         if (err < 0) {
258                 dev_err(&pdev->dev, "Can't get %d IRQ for pwrkey: %d\n",
259                                                                 key_irq, err);
260                 goto unreg_input_dev;
261         }
262         /*
263         #if RICOH619_ONKEY_OFF_IRQ
264         err = request_threaded_irq( key_irq +RICOH619_ONKEY_OFF_IRQ, NULL,pwrkey_irq_off, IRQF_ONESHOT,
265                                                 "ricoh619_pwrkey_off", pwrkey);
266         if (err < 0) {
267                 dev_err(&pdev->dev, "Can't get %d IRQ for ricoh619_pwrkey_off: %d\n",
268                         key_irq + RICOH619_ONKEY_OFF_IRQ, err);
269                 free_irq(key_irq, pwrkey);
270                 goto unreg_input_dev;
271         }
272         #endif
273 */
274         pwrkey->workqueue = create_singlethread_workqueue("ricoh619_pwrkey");
275         INIT_WORK(&pwrkey->work, ricoh619_irq_work);
276
277         /* Enable power key IRQ */
278         /* trigger both edge */
279         ricoh619_set_bits(pwrkey->dev->parent, RICOH619_PWR_IRSEL, 0x1);
280         /* Enable system interrupt */
281         ricoh619_set_bits(pwrkey->dev->parent, RICOH619_INTC_INTEN, 0x1);
282         /* Enable power-on interrupt */
283         ricoh619_set_bits(pwrkey->dev->parent, RICOH619_INT_EN_SYS, 0x1);
284 //      printk(KERN_INFO "PMU: %s is OK!\n", __func__);
285         return 0;
286
287 unreg_input_dev:
288         input_unregister_device(pwr);
289         pwr = NULL;
290
291 free_input_dev:
292         input_free_device(pwr);
293         free_pwrkey:
294         kfree(pwrkey);
295
296         return err;
297 }
298
299 static int ricoh619_pwrkey_remove(struct platform_device *pdev)
300 {
301         struct ricoh619_pwrkey *pwrkey = platform_get_drvdata(pdev);
302
303         flush_workqueue(pwrkey->workqueue);
304         destroy_workqueue(pwrkey->workqueue);
305         free_irq(pwrkey->key_irq, pwrkey);
306         input_unregister_device(pwrkey->pwr);
307         kfree(pwrkey);
308
309         return 0;
310 }
311
312 #ifdef CONFIG_PM
313 static int ricoh619_pwrkey_suspend(struct device *dev)
314 {
315         struct ricoh619_pwrkey *info = dev_get_drvdata(dev);
316
317 //      printk(KERN_INFO "PMU: %s\n", __func__);
318
319 //      if (info->key_irq)
320 //              disable_irq(info->key_irq);
321         cancel_work_sync(&info->work);
322         flush_workqueue(info->workqueue);
323
324         return 0;
325 }
326
327 static int ricoh619_pwrkey_resume(struct device *dev)
328 {
329         struct ricoh619_pwrkey *info = dev_get_drvdata(dev);
330
331 //      printk(KERN_INFO "PMU: %s\n", __func__);
332         queue_work(info->workqueue, &info->work);
333 //      if (info->key_irq)
334 //              enable_irq(info->key_irq);
335
336         return 0;
337 }
338
339 #ifdef CONFIG_OF
340 static const struct of_device_id ricoh619_pwrkey_dt_match[] = {
341         { .compatible = "ricoh,ricoh619-pwrkey", },
342         {},
343 };
344 MODULE_DEVICE_TABLE(of, ricoh619_pwrkey_dt_match);
345 #endif
346
347 static const struct dev_pm_ops ricoh619_pwrkey_pm_ops = {
348         .suspend        = ricoh619_pwrkey_suspend,
349         .resume         = ricoh619_pwrkey_resume,
350 };
351 #endif
352
353 static struct platform_driver ricoh619_pwrkey_driver = {
354         .probe = ricoh619_pwrkey_probe,
355         .remove = ricoh619_pwrkey_remove,
356         .driver = {
357                 .name = "ricoh619-pwrkey",
358                 .owner = THIS_MODULE,
359                 .of_match_table = of_match_ptr(ricoh619_pwrkey_dt_match),
360 #ifdef CONFIG_PM
361                 .pm     = &ricoh619_pwrkey_pm_ops,
362 #endif
363         },
364 };
365
366 static int __init ricoh619_pwrkey_init(void)
367 {
368         return platform_driver_register(&ricoh619_pwrkey_driver);
369 }
370 subsys_initcall_sync(ricoh619_pwrkey_init);
371
372 static void __exit ricoh619_pwrkey_exit(void)
373 {
374         platform_driver_unregister(&ricoh619_pwrkey_driver);
375 }
376 module_exit(ricoh619_pwrkey_exit);
377
378
379 MODULE_ALIAS("platform:ricoh619-pwrkey");
380 MODULE_AUTHOR("zhangqing <zhangqing@rock-chips.com>");
381 MODULE_DESCRIPTION("ricoh619 Power Key");
382 MODULE_LICENSE("GPL v2");