input: touchscreen: add touch screen of gslx680 for rk3399-firefly-edp
[firefly-linux-kernel-4.4.55.git] / drivers / input / keyboard / wm831x_gpio_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/workqueue.h>
25
26 #include <asm/gpio.h>
27
28 #include <linux/mfd/wm831x/core.h>
29 #include <linux/mfd/wm831x/pdata.h>
30
31 #define CONFIG_WM831X_GPIO_KEY_DEBUG      0
32
33 #if (CONFIG_WM831X_GPIO_KEY_DEBUG)
34 #define WM831X_GPIO_KEY_DG(format, ...)      printk(format, ## __VA_ARGS__)
35 #else
36 #define WM831X_GPIO_KEY_DG(format, ...)
37 #endif
38 bool isHSKeyMIC = false;
39 int pre_state = 0;
40 struct wm831x_gpio_keys_button *media_button;
41
42 extern bool wm8994_set_status(void);
43 extern int headset_status(void);
44
45 struct wm831x_gpio_button_data {
46         struct wm831x_gpio_keys_button *button;
47         struct input_dev *input;
48         struct timer_list timer;
49         struct work_struct work;
50 };
51
52 struct wm831x_gpio_keys_drvdata {
53         struct input_dev *input;
54         struct wm831x_gpio_button_data data[0];
55 };
56
57 bool isHSKey_MIC(void)
58 {           
59         return isHSKeyMIC;
60 }
61 EXPORT_SYMBOL_GPL(isHSKey_MIC);
62
63 void detect_HSMic(void)
64 {
65         int state;
66         struct wm831x_gpio_keys_button *button = media_button;
67         WM831X_GPIO_KEY_DG("detect_HSMic\n");
68         if(!headset_status())
69         {
70                 isHSKeyMIC = false;
71                 return ;
72         }
73         else
74         {
75                 mdelay(500);
76         }
77         
78         state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
79
80         WM831X_GPIO_KEY_DG("detect_HSMic: code=%d,gpio=%d\n",button->gpio,button->code);
81         if(state){
82                 WM831X_GPIO_KEY_DG("detect_HSMic:---headset without MIC and HSKey---\n");
83                 isHSKeyMIC = false;
84         }else{
85                 WM831X_GPIO_KEY_DG("detect_HSMic:---headset with MIC---\n");
86                 isHSKeyMIC = true;
87         }
88
89         return;
90 }
91 EXPORT_SYMBOL_GPL(detect_HSMic);
92
93 static int HSKeyDetect(int state)
94 {
95         WM831X_GPIO_KEY_DG("HSKeyDetect\n");
96
97         if(headset_status()){
98                 WM831X_GPIO_KEY_DG("headset_status()  == true !\n");
99                 if(pre_state != state && !wm8994_set_status()){
100                         WM831X_GPIO_KEY_DG("wm8994_set_status()  == true !\n");
101                         pre_state = state;
102                         
103                         if(!isHSKeyMIC){
104                                 state = -1;
105                         }
106                 }
107                 else{
108                         WM831X_GPIO_KEY_DG("wm8994_set_status()  == false !\n");
109                         state = -1;
110                 }
111         }
112         else{
113                 WM831X_GPIO_KEY_DG("headset_status()  == false !\n");
114                 isHSKeyMIC = false;
115                 state = -1;
116         }
117
118         return state;
119 }
120
121 static void wm831x_gpio_keys_report_event(struct work_struct *work)
122 {
123         struct wm831x_gpio_button_data *bdata =
124                 container_of(work, struct wm831x_gpio_button_data, work);
125         struct wm831x_gpio_keys_button *button = bdata->button;
126         struct input_dev *input = bdata->input;
127         unsigned int type = button->type ?: EV_KEY;
128         
129         int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
130         if(button->code == KEY_MEDIA)
131         {
132                 state = HSKeyDetect(state);
133                 
134                 if(state == -1)
135                 {
136                         WM831X_GPIO_KEY_DG("wm831x_gpio_keys_report_event:HSKeyDetect=-1\n");
137                         goto out;
138                 }
139         }
140         printk("wm831x_gpio_keys_report_event:state=%d,code=%d \n",state,button->code);
141         
142         input_event(input, type, button->code, state);
143         input_sync(input);
144 out:    
145         enable_irq(gpio_to_irq(button->gpio));
146         return;
147 }
148
149 static void wm831x_gpio_keys_timer(unsigned long _data)
150 {
151         struct wm831x_gpio_button_data *data = (struct wm831x_gpio_button_data *)_data;
152
153         WM831X_GPIO_KEY_DG("wm831x_gpio_keys_timer\n");
154         schedule_work(&data->work);
155 }
156
157 static irqreturn_t wm831x_gpio_keys_isr(int irq, void *dev_id)
158 {
159         struct wm831x_gpio_button_data *bdata = dev_id;
160         struct wm831x_gpio_keys_button *button = bdata->button;
161         
162         //printk("wm831x_gpio_keys_isr:irq=%d,%d \n",irq,button->debounce_interval);
163         
164         BUG_ON(irq != gpio_to_irq(button->gpio));
165         disable_irq_nosync(gpio_to_irq(button->gpio));
166         if (button->debounce_interval)
167                 mod_timer(&bdata->timer,
168                         jiffies + msecs_to_jiffies(button->debounce_interval));
169         else
170                 schedule_work(&bdata->work);
171                 
172         return IRQ_HANDLED;
173 }
174
175 static int __devinit wm831x_gpio_keys_probe(struct platform_device *pdev)
176 {
177
178         struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
179         struct wm831x_pdata *pdata = wm831x->dev->platform_data;
180         struct wm831x_gpio_keys_pdata *gpio_keys;
181         struct wm831x_gpio_keys_drvdata *ddata;
182         struct input_dev *input;
183         int i, error;
184         //int wakeup = 0;
185
186         printk("wm831x_gpio_keys_probe\n");
187         
188         if (pdata == NULL || pdata->gpio_keys == NULL)
189                 return -ENODEV;
190
191         gpio_keys = pdata->gpio_keys;
192         
193         ddata = kzalloc(sizeof(struct wm831x_gpio_keys_drvdata) +
194                         gpio_keys->nbuttons * sizeof(struct wm831x_gpio_button_data),
195                         GFP_KERNEL);
196         input = input_allocate_device();
197         if (!ddata || !input) {
198                 error = -ENOMEM;
199                 goto fail1;
200         }
201         
202         platform_set_drvdata(pdev, ddata);
203
204         input->name = pdev->name;
205         input->phys = "wm831x_gpio-keys/input0";
206         input->dev.parent = &pdev->dev;
207
208         input->id.bustype = BUS_I2C;
209         input->id.vendor = 0x0001;
210         input->id.product = 0x0001;
211         input->id.version = 0x0100;
212
213         /* Enable auto repeat feature of Linux input subsystem */
214         if (gpio_keys->rep)
215                 __set_bit(EV_REP, input->evbit);
216
217         ddata->input = input;
218         
219         for (i = 0; i < gpio_keys->nbuttons; i++) {
220                 struct wm831x_gpio_keys_button *button = &gpio_keys->buttons[i];
221                 struct wm831x_gpio_button_data *bdata = &ddata->data[i];
222                 int irq = 0;
223                 unsigned int type = button->type ?: EV_KEY;
224
225                 bdata->input = input;
226                 bdata->button = button;
227                 
228                 if(button->code == KEY_MEDIA)
229                 {
230                         media_button = button;
231                 }
232                 if (button->debounce_interval)
233                         setup_timer(&bdata->timer,
234                                         wm831x_gpio_keys_timer, (unsigned long)bdata);
235                 //else
236                 INIT_WORK(&bdata->work, wm831x_gpio_keys_report_event);
237
238                 error = gpio_request(button->gpio, button->desc ?: "wm831x_gpio_keys");
239                 if (error < 0) {
240                         pr_err("wm831x_gpio-keys: failed to request GPIO %d,"
241                                 " error %d\n", button->gpio, error);
242                         goto fail2;
243                 }
244
245                 if(button->gpio >= WM831X_P01 && button->gpio <= WM831X_P12)
246                 {
247                         error = gpio_pull_updown(button->gpio,GPIOPullUp);
248                         if (error < 0) {
249                                 pr_err("wm831x_gpio-keys: failed to pull up"
250                                         " for GPIO %d, error %d\n",
251                                         button->gpio, error);
252                                 gpio_free(button->gpio);
253                                 goto fail2;
254                         }
255                 }
256                 
257                 error = gpio_direction_input(button->gpio);
258                 if (error < 0) {
259                         pr_err("wm831x_gpio-keys: failed to configure input"
260                                 " direction for GPIO %d, error %d\n",
261                                 button->gpio, error);
262                         gpio_free(button->gpio);
263                         goto fail2;
264                 }
265
266                 irq = gpio_to_irq(button->gpio);
267                 if (irq < 0) {
268                         error = irq;
269                         pr_err("wm831x_gpio-keys: Unable to get irq number"
270                                 " for GPIO %d, error %d\n",
271                                 button->gpio, error);
272                         gpio_free(button->gpio);
273                         goto fail2;
274                 }
275                 printk("wm831x_gpio_keys_probe:i=%d,gpio=%d,irq=%d \n",i,button->gpio,irq);
276                 enable_irq_wake(irq);   
277                 if(button->gpio >= WM831X_P01 && button->gpio <= WM831X_P12)
278                 {
279                         error = request_threaded_irq(irq, NULL,wm831x_gpio_keys_isr,
280                                             IRQF_SHARED |
281                                             IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
282                                             button->desc ? button->desc : "wm831x_gpio_keys",
283                                             bdata);
284                 }
285                 else if(button->gpio >= TCA6424_P00 && button->gpio <= TCA6424_P27)
286                 {
287                         error = request_irq(irq, wm831x_gpio_keys_isr,
288                                     IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
289                                     button->desc ? button->desc : "tca6424_gpio_keys",
290                                     bdata);
291                 }
292                 
293                 if (error) {
294                         pr_err("wm831x_gpio-keys: Unable to claim irq %d; error %d\n",
295                                 irq, error);
296                         gpio_free(button->gpio);
297                         goto fail2;
298                 }
299
300                 //if (button->wakeup)
301                 //      wakeup = 1;
302
303                 input_set_capability(input, type, button->code);
304         }
305
306         error = input_register_device(input);
307         if (error) {
308                 pr_err("wm831x_gpio-keys: Unable to register input device, "
309                         "error: %d\n", error);
310                 goto fail2;
311         }
312
313         //device_init_wakeup(&pdev->dev, wakeup);
314
315         return 0;
316
317  fail2:
318         while (--i >= 0) {
319                 free_irq(gpio_to_irq(gpio_keys->buttons[i].gpio), &ddata->data[i]);
320                 if (gpio_keys->buttons[i].debounce_interval)
321                         del_timer_sync(&ddata->data[i].timer);
322                 //else
323                         cancel_work_sync(&ddata->data[i].work);
324                 gpio_free(gpio_keys->buttons[i].gpio);
325         }
326
327         platform_set_drvdata(pdev, NULL);
328  fail1:
329         input_free_device(input);
330         kfree(ddata);
331
332         return error;
333 }
334
335 static int __devexit wm831x_gpio_keys_remove(struct platform_device *pdev)
336 {
337         struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
338         struct wm831x_pdata *pdata = wm831x->dev->platform_data;
339         struct wm831x_gpio_keys_pdata *gpio_keys;
340         struct wm831x_gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
341         struct input_dev *input = ddata->input;
342         int i;
343         
344         if (pdata == NULL || pdata->gpio_keys == NULL)
345                 return -ENODEV;
346         
347         gpio_keys = pdata->gpio_keys;
348         
349         //device_init_wakeup(&pdev->dev, 0);
350
351         for (i = 0; i < gpio_keys->nbuttons; i++) {
352                 int irq = gpio_to_irq(gpio_keys->buttons[i].gpio);
353                 free_irq(irq, &ddata->data[i]);
354                 if (gpio_keys->buttons[i].debounce_interval)
355                         del_timer_sync(&ddata->data[i].timer);
356                 //else
357                         cancel_work_sync(&ddata->data[i].work);
358                 gpio_free(gpio_keys->buttons[i].gpio);
359         }
360
361         input_unregister_device(input);
362
363         return 0;
364 }
365
366 #ifdef CONFIG_PM
367 static int wm831x_gpio_keys_suspend(struct device *dev)
368 {
369         struct platform_device *pdev = to_platform_device(dev);
370         struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
371         struct wm831x_pdata *pdata = wm831x->dev->platform_data;
372         struct wm831x_gpio_keys_pdata *gpio_keys;
373         int i,irq;
374
375         if (pdata == NULL || pdata->gpio_keys == NULL)
376         {
377                 printk("wm831x_gpio_keys_suspend fail\n");
378                 return -ENODEV;
379         }       
380
381         //printk("wm831x_gpio_keys_suspend\n");
382         
383         gpio_keys = pdata->gpio_keys;
384         
385         //if (device_may_wakeup(&pdev->dev)) {
386                 for (i = 0; i < gpio_keys->nbuttons; i++) {
387                         struct wm831x_gpio_keys_button *button = &gpio_keys->buttons[i];
388                         if (button->wakeup) {
389                                 irq = gpio_to_irq(button->gpio);
390                                 enable_irq_wake(irq);                   
391                         }
392                         else
393                         {
394                                 irq = gpio_to_irq(button->gpio);
395                                 disable_irq_wake(irq);
396                         }
397                 }
398         //}
399
400         return 0;
401 }
402
403 static int wm831x_gpio_keys_resume(struct device *dev)
404 {
405         struct platform_device *pdev = to_platform_device(dev);
406         struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
407         struct wm831x_pdata *pdata = wm831x->dev->platform_data;
408         struct wm831x_gpio_keys_pdata *gpio_keys;
409         int i,irq;
410
411         if (pdata == NULL || pdata->gpio_keys == NULL)
412         {
413                 printk("wm831x_gpio_keys_resume fail\n");
414                 return -ENODEV;
415         }
416         
417         //printk("wm831x_gpio_keys_resume\n");
418         
419         gpio_keys = pdata->gpio_keys;
420         
421         //if (device_may_wakeup(&pdev->dev)) {
422                 for (i = 0; i < gpio_keys->nbuttons; i++) {
423                         struct wm831x_gpio_keys_button *button = &gpio_keys->buttons[i];
424                         //if (button->wakeup) {         
425                                 irq = gpio_to_irq(button->gpio);
426                                 enable_irq_wake(irq);
427                         //}
428                 }
429         //}
430
431         return 0;
432 }
433
434 static const struct dev_pm_ops wm831x_gpio_keys_pm_ops = {
435         .suspend        = wm831x_gpio_keys_suspend,
436         .resume         = wm831x_gpio_keys_resume,
437 };              
438 #endif
439
440 static struct platform_driver wm831x_gpio_keys_device_driver = {
441         .probe          = wm831x_gpio_keys_probe,
442         .remove         = __devexit_p(wm831x_gpio_keys_remove),
443         .driver         = {
444                 .name   = "wm831x_gpio-keys",
445                 .owner  = THIS_MODULE,
446 #ifdef CONFIG_PM
447                 .pm     = &wm831x_gpio_keys_pm_ops,
448 #endif
449         }
450 };
451
452 static int __init wm831x_gpio_keys_init(void)
453 {
454         return platform_driver_register(&wm831x_gpio_keys_device_driver);
455 }
456
457 static void __exit wm831x_gpio_keys_exit(void)
458 {
459         platform_driver_unregister(&wm831x_gpio_keys_device_driver);
460 }
461
462 subsys_initcall(wm831x_gpio_keys_init);
463 module_exit(wm831x_gpio_keys_exit);
464
465 MODULE_LICENSE("GPL");
466 MODULE_AUTHOR("SRT <srt@rock-chip.com>");
467 MODULE_DESCRIPTION("Keyboard driver for WM831x GPIOs");
468 MODULE_ALIAS("platform:wm831x_gpio-keys");