add one interface to sending KEY_POWER event in rk29_key.c
[firefly-linux-kernel-4.4.55.git] / drivers / input / keyboard / rk29_keys.c
1 /*
2  * Driver for keys on GPIO lines capable of generating interrupts.
3  *
4  * Copyright 2005 Phil Blundell
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/module.h>
12
13 #include <linux/init.h>
14 #include <linux/fs.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/sched.h>
18 #include <linux/pm.h>
19 #include <linux/sysctl.h>
20 #include <linux/proc_fs.h>
21 #include <linux/delay.h>
22 #include <linux/platform_device.h>
23 #include <linux/input.h>
24 #include <linux/adc.h>
25
26 #include <asm/gpio.h>
27 #include <mach/key.h>
28 #include <mach/board.h>
29
30
31 #define EMPTY_ADVALUE                                   950
32 #define DRIFT_ADVALUE                                   70
33 #define INVALID_ADVALUE                                 10
34
35
36 #if 0
37 #define key_dbg(bdata, format, arg...)          \
38         dev_printk(KERN_INFO , &bdata->input->dev , format , ## arg)
39 #else
40 #define key_dbg(bdata, format, arg...)  
41 #endif
42
43 struct rk29_button_data {
44         int state;
45         int long_press_count;
46         struct rk29_keys_button *button;
47         struct input_dev *input;
48         struct timer_list timer;
49 };
50
51 struct rk29_keys_drvdata {
52         int nbuttons;
53         int result;
54         struct input_dev *input;
55         struct adc_client *client;
56         struct timer_list timer;
57         struct rk29_button_data data[0];
58 };
59
60 static struct input_dev *input_dev;
61
62 void rk29_send_power_key(void)
63 {
64         if (!input_dev)
65                 return;
66
67         input_report_key(input_dev, KEY_POWER, 1);
68         input_sync(input_dev);
69         input_report_key(input_dev, KEY_POWER, 0);
70         input_sync(input_dev);
71 }
72
73 void rk28_send_wakeup_key(void)
74 {
75         if (!input_dev)
76                 return;
77
78         input_report_key(input_dev, KEY_WAKEUP, 1);
79         input_sync(input_dev);
80         input_report_key(input_dev, KEY_WAKEUP, 0);
81         input_sync(input_dev);
82 }
83
84 static void keys_long_press_timer(unsigned long _data)
85 {
86         int state;
87         struct rk29_button_data *bdata = (struct rk29_button_data *)_data;
88         struct rk29_keys_button *button = bdata->button;
89         struct input_dev *input = bdata->input;
90         unsigned int type = EV_KEY;
91         if(button->gpio != INVALID_GPIO )
92                 state = !!((gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low);
93         else
94                 state = !!button->adc_state;
95         if(state) {
96                 if(bdata->long_press_count != 0) {
97                         if(bdata->long_press_count % (LONG_PRESS_COUNT+ONE_SEC_COUNT) == 0){
98                                 key_dbg(bdata, "%skey[%s]: report ev[%d] state[0]\n", 
99                                         (button->gpio == INVALID_GPIO)?"ad":"io", button->desc, button->code_long_press);
100                                 input_event(input, type, button->code_long_press, 0);
101                                 input_sync(input);
102                         }
103                         else if(bdata->long_press_count%LONG_PRESS_COUNT == 0) {
104                                 key_dbg(bdata, "%skey[%s]: report ev[%d] state[1]\n", 
105                                         (button->gpio == INVALID_GPIO)?"ad":"io", button->desc, button->code_long_press);
106                                 input_event(input, type, button->code_long_press, 1);
107                                 input_sync(input);
108                         }
109                 }
110                 bdata->long_press_count++;
111                 mod_timer(&bdata->timer,
112                                 jiffies + msecs_to_jiffies(DEFAULT_DEBOUNCE_INTERVAL));
113         }
114         else {
115                 if(bdata->long_press_count <= LONG_PRESS_COUNT) {
116                         bdata->long_press_count = 0;
117                         key_dbg(bdata, "%skey[%s]: report ev[%d] state[1], report ev[%d] state[0]\n", 
118                                         (button->gpio == INVALID_GPIO)?"ad":"io", button->desc, button->code, button->code);
119                         input_event(input, type, button->code, 1);
120                         input_sync(input);
121                         input_event(input, type, button->code, 0);
122                         input_sync(input);
123                 }
124                 else if(bdata->state != state) {
125                         key_dbg(bdata, "%skey[%s]: report ev[%d] state[0]\n", 
126                         (button->gpio == INVALID_GPIO)?"ad":"io", button->desc, button->code_long_press);
127                         input_event(input, type, button->code_long_press, 0);
128                         input_sync(input);
129                 }
130         }
131         bdata->state = state;
132 }
133 static void keys_timer(unsigned long _data)
134 {
135         int state;
136         struct rk29_button_data *bdata = (struct rk29_button_data *)_data;
137         struct rk29_keys_button *button = bdata->button;
138         struct input_dev *input = bdata->input;
139         unsigned int type = EV_KEY;
140         
141         if(button->gpio != INVALID_GPIO)
142                 state = !!((gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low);
143         else
144                 state = !!button->adc_state;
145         if(bdata->state != state) {
146                 bdata->state = state;
147                 key_dbg(bdata, "%skey[%s]: report ev[%d] state[%d]\n", 
148                         (button->gpio == INVALID_GPIO)?"ad":"io", button->desc, button->code, bdata->state);
149                 input_event(input, type, button->code, bdata->state);
150                 input_sync(input);
151         }
152         if(state)
153                 mod_timer(&bdata->timer,
154                         jiffies + msecs_to_jiffies(DEFAULT_DEBOUNCE_INTERVAL));
155 }
156
157 static irqreturn_t keys_isr(int irq, void *dev_id)
158 {
159         struct rk29_button_data *bdata = dev_id;
160         struct rk29_keys_button *button = bdata->button;
161         BUG_ON(irq != gpio_to_irq(button->gpio));
162         bdata->long_press_count = 0;
163         mod_timer(&bdata->timer,
164                                 jiffies + msecs_to_jiffies(DEFAULT_DEBOUNCE_INTERVAL));
165         return IRQ_HANDLED;
166 }
167 static void callback(struct adc_client *client, void *client_param, int result)
168 {
169         struct rk29_keys_drvdata *ddata = (struct rk29_keys_drvdata *)client_param;
170         int i;
171         if(result > INVALID_ADVALUE && result < EMPTY_ADVALUE)
172                 ddata->result = result;
173         for (i = 0; i < ddata->nbuttons; i++) {
174                 struct rk29_button_data *bdata = &ddata->data[i];
175                 struct rk29_keys_button *button = bdata->button;
176                 if(!button->adc_value)
177                         continue;
178                 if(result < button->adc_value + DRIFT_ADVALUE &&
179                         result > button->adc_value - DRIFT_ADVALUE)
180                         button->adc_state = 1;
181                 else
182                         button->adc_state = 0;
183                 if(bdata->state != button->adc_state)
184                         mod_timer(&bdata->timer,
185                                 jiffies + msecs_to_jiffies(DEFAULT_DEBOUNCE_INTERVAL));
186         }
187         return;
188 }
189 static void adc_timer(unsigned long _data)
190 {
191         struct rk29_keys_drvdata *ddata = (struct rk29_keys_drvdata *)_data;
192         
193         adc_async_read(ddata->client);
194         mod_timer(&ddata->timer, jiffies + msecs_to_jiffies(ADC_SAMPLE_TIME));
195 }
196 static ssize_t adc_value_show(struct device *dev, struct device_attribute *attr, char *buf)
197 {
198         struct rk29_keys_drvdata *ddata = dev_get_drvdata(dev);
199         
200         return sprintf(buf, "adc_value: %d\n", ddata->result);
201 }
202
203 static DEVICE_ATTR(get_adc_value, S_IRUGO | S_IWUSR, adc_value_show, NULL);
204
205 static int __devinit keys_probe(struct platform_device *pdev)
206 {
207         struct rk29_keys_platform_data *pdata = pdev->dev.platform_data;
208         struct rk29_keys_drvdata *ddata;
209         struct input_dev *input;
210         int i, error = 0;
211         int wakeup = 0;
212
213         if(!pdata) 
214                 return -EINVAL;
215         
216         ddata = kzalloc(sizeof(struct rk29_keys_drvdata) +
217                         pdata->nbuttons * sizeof(struct rk29_button_data),
218                         GFP_KERNEL);
219         input = input_allocate_device();
220         if (!ddata || !input) {
221                 error = -ENOMEM;
222                 goto fail0;
223         }
224
225         platform_set_drvdata(pdev, ddata);
226
227         input->name = pdev->name;
228         input->phys = "gpio-keys/input0";
229         input->dev.parent = &pdev->dev;
230
231         input->id.bustype = BUS_HOST;
232         input->id.vendor = 0x0001;
233         input->id.product = 0x0001;
234         input->id.version = 0x0100;
235
236         /* Enable auto repeat feature of Linux input subsystem */
237         if (pdata->rep)
238                 __set_bit(EV_REP, input->evbit);
239         ddata->nbuttons = pdata->nbuttons;
240         ddata->input = input;
241         if(pdata->chn >= 0) {
242                 ddata->client = adc_register(pdata->chn, callback, (void *)ddata);
243                 if(!ddata->client) {
244                         error = -EINVAL;
245                         goto fail1;
246                 }
247                 setup_timer(&ddata->timer,
248                                 adc_timer, (unsigned long)ddata);
249                 mod_timer(&ddata->timer, jiffies + msecs_to_jiffies(100));
250         }
251         for (i = 0; i < pdata->nbuttons; i++) {
252                 struct rk29_keys_button *button = &pdata->buttons[i];
253                 struct rk29_button_data *bdata = &ddata->data[i];
254                 int irq;
255                 unsigned int type = EV_KEY;
256
257                 bdata->input = input;
258                 bdata->button = button;
259                 if(button->code_long_press)
260                         setup_timer(&bdata->timer,
261                                 keys_long_press_timer, (unsigned long)bdata);
262                 else if(button->code)
263                         setup_timer(&bdata->timer,
264                                 keys_timer, (unsigned long)bdata);
265                 if(button->gpio != INVALID_GPIO) {
266                         error = gpio_request(button->gpio, button->desc ?: "keys");
267                         if (error < 0) {
268                                 pr_err("gpio-keys: failed to request GPIO %d,"
269                                         " error %d\n", button->gpio, error);
270                                 goto fail2;
271                         }
272
273                         error = gpio_direction_input(button->gpio);
274                         if (error < 0) {
275                                 pr_err("gpio-keys: failed to configure input"
276                                         " direction for GPIO %d, error %d\n",
277                                         button->gpio, error);
278                                 gpio_free(button->gpio);
279                                 goto fail2;
280                         }
281
282                         irq = gpio_to_irq(button->gpio);
283                         if (irq < 0) {
284                                 error = irq;
285                                 pr_err("gpio-keys: Unable to get irq number"
286                                         " for GPIO %d, error %d\n",
287                                         button->gpio, error);
288                                 gpio_free(button->gpio);
289                                 goto fail2;
290                         }
291
292                         error = request_irq(irq, keys_isr,
293                                             (button->active_low)?IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING,
294                                             button->desc ? button->desc : "keys",
295                                             bdata);
296                         if (error) {
297                                 pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
298                                         irq, error);
299                                 gpio_free(button->gpio);
300                                 goto fail2;
301                         }
302                 }
303                 if (button->wakeup)
304                         wakeup = 1;
305
306                 input_set_capability(input, type, button->code);
307         }
308
309         input_set_capability(input, EV_KEY, KEY_WAKEUP);
310
311         error = input_register_device(input);
312         if (error) {
313                 pr_err("gpio-keys: Unable to register input device, "
314                         "error: %d\n", error);
315                 goto fail2;
316         }
317
318         device_init_wakeup(&pdev->dev, wakeup);
319         error = device_create_file(&pdev->dev, &dev_attr_get_adc_value);
320
321         input_dev = input;
322         return error;
323
324  fail2:
325         while (--i >= 0) {
326                 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
327                 del_timer_sync(&ddata->data[i].timer);
328                 gpio_free(pdata->buttons[i].gpio);
329         }
330         if(pdata->chn >= 0 && ddata->client);
331                 adc_unregister(ddata->client);
332  fail1:
333         platform_set_drvdata(pdev, NULL);
334  fail0:
335         input_free_device(input);
336         kfree(ddata);
337
338         return error;
339 }
340
341 static int __devexit keys_remove(struct platform_device *pdev)
342 {
343         struct rk29_keys_platform_data *pdata = pdev->dev.platform_data;
344         struct rk29_keys_drvdata *ddata = platform_get_drvdata(pdev);
345         struct input_dev *input = ddata->input;
346         int i;
347
348         input_dev = NULL;
349         device_init_wakeup(&pdev->dev, 0);
350
351         for (i = 0; i < pdata->nbuttons; i++) {
352                 int irq = gpio_to_irq(pdata->buttons[i].gpio);
353                 free_irq(irq, &ddata->data[i]);
354                 del_timer_sync(&ddata->data[i].timer);
355                 gpio_free(pdata->buttons[i].gpio);
356         }
357         if(pdata->chn >= 0 && ddata->client);
358                 adc_unregister(ddata->client);
359         input_unregister_device(input);
360
361         return 0;
362 }
363
364
365 #ifdef CONFIG_PM
366 static int keys_suspend(struct device *dev)
367 {
368         struct platform_device *pdev = to_platform_device(dev);
369         struct rk29_keys_platform_data *pdata = pdev->dev.platform_data;
370         int i;
371
372         if (device_may_wakeup(&pdev->dev)) {
373                 for (i = 0; i < pdata->nbuttons; i++) {
374                         struct rk29_keys_button *button = &pdata->buttons[i];
375                         if (button->wakeup) {
376                                 int irq = gpio_to_irq(button->gpio);
377                                 enable_irq_wake(irq);
378                         }
379                 }
380         }
381
382         return 0;
383 }
384
385 static int keys_resume(struct device *dev)
386 {
387         struct platform_device *pdev = to_platform_device(dev);
388         struct rk29_keys_platform_data *pdata = pdev->dev.platform_data;
389         int i;
390
391         if (device_may_wakeup(&pdev->dev)) {
392                 for (i = 0; i < pdata->nbuttons; i++) {
393                         struct rk29_keys_button *button = &pdata->buttons[i];
394                         if (button->wakeup) {
395                                 int irq = gpio_to_irq(button->gpio);
396                                 disable_irq_wake(irq);
397                         }
398                 }
399         }
400
401         return 0;
402 }
403
404 static const struct dev_pm_ops keys_pm_ops = {
405         .suspend        = keys_suspend,
406         .resume         = keys_resume,
407 };
408 #endif
409
410 static struct platform_driver keys_device_driver = {
411         .probe          = keys_probe,
412         .remove         = __devexit_p(keys_remove),
413         .driver         = {
414                 .name   = "rk29-keypad",
415                 .owner  = THIS_MODULE,
416 #ifdef CONFIG_PM
417                 .pm     = &keys_pm_ops,
418 #endif
419         }
420 };
421
422 static int __init keys_init(void)
423 {
424         return platform_driver_register(&keys_device_driver);
425 }
426
427 static void __exit keys_exit(void)
428 {
429         platform_driver_unregister(&keys_device_driver);
430 }
431
432 module_init(keys_init);
433 module_exit(keys_exit);
434
435 MODULE_LICENSE("GPL");
436 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
437 MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
438 MODULE_ALIAS("platform:gpio-keys");