rk2928phonepad:
[firefly-linux-kernel-4.4.55.git] / drivers / headset_observe / rk_headset.c
1 /* arch/arm/mach-rockchip/rk28_headset.c
2  *
3  * Copyright (C) 2009 Rockchip Corporation.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15
16 #include <linux/module.h>
17 #include <linux/sysdev.h>
18 #include <linux/device.h>
19 #include <linux/fs.h>
20 #include <linux/interrupt.h>
21 #include <linux/workqueue.h>
22 #include <linux/irq.h>
23 #include <linux/delay.h>
24 #include <linux/types.h>
25 #include <linux/input.h>
26 #include <linux/platform_device.h>
27 #include <linux/mutex.h>
28 #include <linux/errno.h>
29 #include <linux/err.h>
30 #include <linux/hrtimer.h>
31 #include <linux/switch.h>
32 #include <linux/input.h>
33 #include <linux/debugfs.h>
34 #include <linux/wakelock.h>
35 #include <asm/gpio.h>
36 #include <asm/atomic.h>
37 #include <asm/mach-types.h>
38 #include "rk_headset.h"
39 #include <linux/earlysuspend.h>
40 #include <linux/gpio.h>
41 #include <mach/board.h>
42 #include <linux/slab.h>
43
44 /* Debug */
45 #if 0
46 #define DBG(x...) printk(x)
47 #else
48 #define DBG(x...) do { } while (0)
49 #endif
50
51 #define BIT_HEADSET             (1 << 0)
52 #define BIT_HEADSET_NO_MIC      (1 << 1)
53
54 #define HEADSET 0
55 #define HOOK 1
56
57 #define HEADSET_IN 1
58 #define HEADSET_OUT 0
59 #define HOOK_DOWN 0
60 #define HOOK_UP 1
61 #define enable 1
62 #define disable 0
63
64 #ifdef CONFIG_SND_RK_SOC_RK2928
65 extern void rk2928_codec_set_spk(bool on);
66 #endif
67 #ifdef CONFIG_SND_SOC_WM8994
68 extern int wm8994_set_status(void);
69 #endif
70
71 /* headset private data */
72 struct headset_priv {
73         struct input_dev *input_dev;
74         struct rk_headset_pdata *pdata;
75         unsigned int headset_status:1;
76         unsigned int hook_status:1;
77         unsigned int isMic:1;
78         unsigned int isHook_irq:1;
79         int cur_headset_status; 
80         
81         unsigned int irq[2];
82         unsigned int irq_type[2];
83         struct delayed_work h_delayed_work[2];
84         struct switch_dev sdev;
85         struct mutex mutex_lock[2];     
86         struct timer_list headset_timer;
87         unsigned char *keycodes;
88 };
89 static struct headset_priv *headset_info;
90
91 int Headset_isMic(void)
92 {
93         return headset_info->isMic;
94 }
95 EXPORT_SYMBOL_GPL(Headset_isMic);
96
97 int Headset_status(void)
98 {
99         if(headset_info->cur_headset_status == BIT_HEADSET_NO_MIC ||
100                 headset_info->cur_headset_status == BIT_HEADSET )
101                 return HEADSET_IN;
102         else
103                 return HEADSET_OUT;
104 }
105 EXPORT_SYMBOL_GPL(Headset_status);
106
107 static irqreturn_t headset_interrupt(int irq, void *dev_id)
108 {
109         DBG("---headset_interrupt---\n");       
110         schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(50));
111         return IRQ_HANDLED;
112 }
113
114 static irqreturn_t Hook_interrupt(int irq, void *dev_id)
115 {
116         DBG("---Hook_interrupt---\n");  
117 //      disable_irq_nosync(headset_info->irq[HOOK]);
118         schedule_delayed_work(&headset_info->h_delayed_work[HOOK], msecs_to_jiffies(100));
119         return IRQ_HANDLED;
120 }
121
122 static int headset_change_irqtype(int type,unsigned int irq_type)
123 {
124         int ret = 0;
125         DBG("--------%s----------\n",__FUNCTION__);
126         free_irq(headset_info->irq[type],NULL);
127         
128         switch(type)
129         {
130         case HOOK:
131                 ret = request_irq(headset_info->irq[type], Hook_interrupt, irq_type, "headset_input", NULL);
132                 break;
133         case HEADSET:
134                 ret = request_irq(headset_info->irq[type], headset_interrupt, irq_type, "headset_hook", NULL);
135                 break;
136         default:
137                 ret = -1;
138                 break;
139         }
140
141         if (ret<0) 
142         {
143                 DBG("headset_change_irqtype: request irq failed\n");
144         return ret;
145         }
146         return ret;
147 }
148
149 static void headsetobserve_work(struct work_struct *work)
150 {
151         int i,level = 0;
152         struct rk_headset_pdata *pdata = headset_info->pdata;
153         static unsigned int old_status = 0;
154         DBG("---headsetobserve_work---\n");
155         mutex_lock(&headset_info->mutex_lock[HEADSET]);
156
157         for(i=0; i<3; i++)
158         {
159                 level = gpio_get_value(pdata->Headset_gpio);
160                 if(level < 0)
161                 {
162                         printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Headset_gpio,i);
163                         msleep(1);
164                         continue;
165                 }
166                 else
167                 break;
168         }
169         if(level < 0)
170         {
171                 printk("%s:get pin level  err!\n",__FUNCTION__);
172                 goto RE_ERROR;
173         }
174
175         old_status = headset_info->headset_status;
176         switch(pdata->headset_in_type)
177         {
178         case HEADSET_IN_HIGH:
179                 if(level > 0)
180                         headset_info->headset_status = HEADSET_IN;
181                 else if(level == 0)
182                         headset_info->headset_status = HEADSET_OUT;     
183                 break;
184         case HEADSET_IN_LOW:
185                 if(level == 0)
186                         headset_info->headset_status = HEADSET_IN;
187                 else if(level > 0)
188                         headset_info->headset_status = HEADSET_OUT;             
189                 break;                  
190         default:
191                 DBG("---- ERROR: on headset headset_in_type error -----\n");
192                 break;                  
193         }
194         if(old_status == headset_info->headset_status)
195         {
196                 DBG("old_status == headset_info->headset_status\n");
197                 goto RE_ERROR;
198         }
199
200         switch(pdata->headset_in_type)
201         {
202         case HEADSET_IN_HIGH:
203                 if(level > 0)
204                 {//in--High level
205                         DBG("--- HEADSET_IN_HIGH headset in HIGH---\n");
206                         headset_info->cur_headset_status = BIT_HEADSET_NO_MIC;
207                         headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);//
208                         if (pdata->Hook_gpio) {
209                                 del_timer(&headset_info->headset_timer);//Start the timer, wait for switch to the headphone channel
210                         //      headset_info->headset_timer.expires = jiffies + 500;
211                                 headset_info->headset_timer.expires = jiffies + 10;
212                                 add_timer(&headset_info->headset_timer);
213                         }
214                 }
215                 else if(level == 0)
216                 {//out--Low level
217                         DBG("---HEADSET_IN_HIGH headset out HIGH---\n");        
218                         if(headset_info->isHook_irq == enable)
219                         {
220                                 DBG("disable headset_hook irq\n");
221                                 headset_info->isHook_irq = disable;
222                                 disable_irq(headset_info->irq[HOOK]);           
223                         }       
224                         headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
225         #ifdef CONFIG_SND_RK_SOC_RK2928
226         rk2928_codec_set_spk(HEADSET_OUT);
227         #endif                                          
228                         headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);//
229                         rk28_send_wakeup_key();
230                         switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);        
231                         DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);                        
232                 }
233                 break;
234         case HEADSET_IN_LOW:
235                 if(level == 0)
236                 {//in--High level
237                         DBG("---HEADSET_IN_LOW headset in LOW ---\n");
238                         headset_info->cur_headset_status = BIT_HEADSET_NO_MIC;
239                         headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);//
240                         if (pdata->Hook_gpio) {                 
241                                 del_timer(&headset_info->headset_timer);//Start the timer, wait for switch to the headphone channel
242                         //      headset_info->headset_timer.expires = jiffies + 500;
243                                 headset_info->headset_timer.expires = jiffies + 10;
244                                 add_timer(&headset_info->headset_timer);
245                         }
246                 }
247                 else if(level > 0)
248                 {//out--High level
249                         DBG("---HEADSET_IN_LOW headset out LOW ---\n");
250                         if(headset_info->isHook_irq == enable)
251                         {
252                                 DBG("disable headset_hook irq\n");
253                                 headset_info->isHook_irq = disable;
254                                 disable_irq(headset_info->irq[HOOK]);           
255                         }
256         #ifdef CONFIG_SND_RK_SOC_RK2928
257         rk2928_codec_set_spk(HEADSET_OUT);
258         #endif          
259                         headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
260                         headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);//
261                         rk28_send_wakeup_key();
262                         switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);        
263                         DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);                        
264                 }
265                 break;                  
266         default:
267                 DBG("---- ERROR: on headset headset_in_type error -----\n");
268                 break;                  
269         }
270         
271
272 RE_ERROR:
273         mutex_unlock(&headset_info->mutex_lock[HEADSET]);       
274 }
275
276 static void Hook_work(struct work_struct *work)
277 {
278         int i,level = 0;
279         struct rk_headset_pdata *pdata = headset_info->pdata;
280         static unsigned int old_status = HOOK_UP;
281
282         DBG("---Hook_work---\n");
283         mutex_lock(&headset_info->mutex_lock[HOOK]);
284         if(headset_info->headset_status == HEADSET_OUT)
285         {
286                 DBG("Headset is out\n");
287                 goto RE_ERROR;
288         }       
289         #ifdef CONFIG_SND_SOC_WM8994
290         if(wm8994_set_status() != 0)
291         {
292                 DBG("wm8994 is not set on heatset channel or suspend\n");
293                 goto RE_ERROR;
294         }
295         #endif
296         for(i=0; i<3; i++)
297         {
298                 level = gpio_get_value(pdata->Hook_gpio);
299                 if(level < 0)
300                 {
301                         printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Hook_gpio,i);
302                         msleep(1);
303                         continue;
304                 }
305                 else
306                 break;
307         }
308         if(level < 0)
309         {
310                 printk("%s:get pin level  err!\n",__FUNCTION__);
311                 goto RE_ERROR;
312         }
313         
314         old_status = headset_info->hook_status;
315         if(level == 0)
316                 headset_info->hook_status = HOOK_UP;
317         else if(level > 0)      
318                 headset_info->hook_status = HOOK_DOWN;
319         if(old_status == headset_info->hook_status)
320         {
321                 DBG("old_status == headset_info->hook_status\n");
322                 goto RE_ERROR;
323         }       
324         
325         if(level == 0)
326         {
327                 DBG("---HOOK Down ---\n");
328                 headset_change_irqtype(HOOK,IRQF_TRIGGER_RISING);//
329                 input_report_key(headset_info->input_dev,pdata->hook_key_code,headset_info->hook_status);
330                 input_sync(headset_info->input_dev);
331         }
332         else if(level > 0)
333         {
334                 DBG("---HOOK Up ---\n");                
335                 headset_change_irqtype(HOOK,IRQF_TRIGGER_FALLING);//
336                 input_report_key(headset_info->input_dev,pdata->hook_key_code,headset_info->hook_status);
337                 input_sync(headset_info->input_dev);
338         }
339 RE_ERROR:
340         mutex_unlock(&headset_info->mutex_lock[HOOK]);
341 }
342
343 static void headset_timer_callback(unsigned long arg)
344 {
345         struct headset_priv *headset = (struct headset_priv *)(arg);
346         struct rk_headset_pdata *pdata = headset->pdata;
347         int i,level = 0;
348         
349 //      printk("headset_timer_callback,headset->headset_status=%d\n",headset->headset_status);  
350
351         if(headset->headset_status == HEADSET_OUT)
352         {
353                 printk("Headset is out\n");
354                 goto out;
355         }
356         #ifdef CONFIG_SND_SOC_WM8994
357         if(wm8994_set_status() != 0)
358         {
359         //      printk("wait wm8994 set the MICB2\n");
360         //      headset_info->headset_timer.expires = jiffies + 500;
361                 headset_info->headset_timer.expires = jiffies + 10;
362                 add_timer(&headset_info->headset_timer);        
363                 goto out;
364         }
365         #endif
366         for(i=0; i<3; i++)
367         {
368                 level = gpio_get_value(pdata->Hook_gpio);
369                 if(level < 0)
370                 {
371                         printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Hook_gpio,i);
372                         msleep(1);
373                         continue;
374                 }
375                 else
376                 break;
377         }
378         if(level < 0)
379         {
380                 printk("%s:get pin level  err!\n",__FUNCTION__);
381                 goto out;
382         }
383
384         if(level == 0)
385         {
386                 headset->isMic= 0;//No microphone
387                 headset_info->cur_headset_status = BIT_HEADSET_NO_MIC;
388                 printk("headset->isMic = %d\n",headset->isMic);         
389         }       
390         else if(level > 0)      
391         {       
392                 headset->isMic = 1;//have mic
393                 DBG("enable headset_hook irq\n");
394                 enable_irq(headset_info->irq[HOOK]);
395                 headset->isHook_irq = enable;
396                 headset_info->cur_headset_status = BIT_HEADSET; 
397                 printk("headset->isMic = %d\n",headset->isMic);         
398         }
399         #ifdef CONFIG_SND_RK_SOC_RK2928
400         rk2928_codec_set_spk(HEADSET_IN);
401         #endif
402         rk28_send_wakeup_key();
403         switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);        
404         DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);        
405
406 out:
407         return;
408 }
409
410 static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
411 {
412         return sprintf(buf, "Headset\n");
413 }
414
415 #ifdef CONFIG_HAS_EARLYSUSPEND
416 static void headset_early_resume(struct early_suspend *h)
417 {
418         schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(10));
419         //DBG(">>>>>headset_early_resume\n");
420 }
421
422 static struct early_suspend hs_early_suspend;
423 #endif
424
425 static int rk_Hskey_open(struct input_dev *dev)
426 {
427         //struct rk28_adckey *adckey = input_get_drvdata(dev);
428 //      DBG("===========rk_Hskey_open===========\n");
429         return 0;
430 }
431
432 static void rk_Hskey_close(struct input_dev *dev)
433 {
434 //      DBG("===========rk_Hskey_close===========\n");
435 //      struct rk28_adckey *adckey = input_get_drvdata(dev);
436
437 }
438
439 static int rockchip_headsetobserve_probe(struct platform_device *pdev)
440 {
441         int ret;
442         struct headset_priv *headset;
443         struct rk_headset_pdata *pdata;
444         
445         headset = kzalloc(sizeof(struct headset_priv), GFP_KERNEL);
446         if (headset == NULL) {
447                 dev_err(&pdev->dev, "failed to allocate driver data\n");
448                 return -ENOMEM;
449         }       
450         headset->pdata = pdev->dev.platform_data;
451         pdata = headset->pdata;
452         headset->headset_status = HEADSET_OUT;
453         headset->hook_status = HOOK_UP;
454         headset->isHook_irq = disable;
455         headset->cur_headset_status = 0;
456         headset->sdev.name = "h2w";
457         headset->sdev.print_name = h2w_print_name;
458         ret = switch_dev_register(&headset->sdev);
459         if (ret < 0)
460                 goto failed_free;
461         
462         mutex_init(&headset->mutex_lock[HEADSET]);
463         mutex_init(&headset->mutex_lock[HOOK]);
464         
465         INIT_DELAYED_WORK(&headset->h_delayed_work[HEADSET], headsetobserve_work);
466         INIT_DELAYED_WORK(&headset->h_delayed_work[HOOK], Hook_work);
467
468 //      init_timer(&headset->headset_timer);
469 //      headset->headset_timer.function = headset_timer_callback;
470 //      headset->headset_timer.data = (unsigned long)headset;
471 //      headset->headset_timer.expires = jiffies + 3000;
472         headset->isMic = 0;
473         setup_timer(&headset->headset_timer, headset_timer_callback, (unsigned long)headset);
474 //      headset->headset_timer.expires = jiffies + 1000;
475 //      add_timer(&headset->headset_timer);     
476         
477         // Create and register the input driver. 
478         headset->input_dev = input_allocate_device();
479         if (!headset->input_dev) {
480                 dev_err(&pdev->dev, "failed to allocate input device\n");
481                 ret = -ENOMEM;
482                 goto failed_free;
483         }       
484         headset->input_dev->name = pdev->name;
485         headset->input_dev->open = rk_Hskey_open;
486         headset->input_dev->close = rk_Hskey_close;
487         headset->input_dev->dev.parent = &pdev->dev;
488         //input_dev->phys = KEY_PHYS_NAME;
489         headset->input_dev->id.vendor = 0x0001;
490         headset->input_dev->id.product = 0x0001;
491         headset->input_dev->id.version = 0x0100;
492         // Register the input device 
493         ret = input_register_device(headset->input_dev);
494         if (ret) {
495                 dev_err(&pdev->dev, "failed to register input device\n");
496                 goto failed_free_dev;
497         }
498
499
500 //      headset->input_dev->keycode = headset->keycodes;
501 //      headset->input_dev->keycodesize = sizeof(unsigned char);
502 //      headset->input_dev->keycodemax = 2;
503         
504 //      set_bit(KEY_MEDIA, headset->input_dev->keybit);
505 //      clear_bit(0, headset->input_dev->keybit);
506         input_set_capability(headset->input_dev, EV_KEY, pdata->hook_key_code);
507 //      input_set_capability(headset->input_dev, EV_SW, SW_HEADPHONE_INSERT);
508 //      input_set_capability(headset->input_dev, EV_KEY, KEY_END);
509
510 //      headset->input_dev->evbit[0] = BIT_MASK(EV_KEY);
511         
512         headset_info = headset;
513         schedule_delayed_work(&headset->h_delayed_work[HEADSET], msecs_to_jiffies(500));        
514
515 #ifdef CONFIG_HAS_EARLYSUSPEND
516         hs_early_suspend.suspend = NULL;
517         hs_early_suspend.resume = headset_early_resume;
518         hs_early_suspend.level = ~0x0;
519         register_early_suspend(&hs_early_suspend);
520 #endif
521
522         //------------------------------------------------------------------
523         if (pdata->Headset_gpio) {
524                 ret = pdata->headset_io_init(pdata->Headset_gpio, pdata->headset_gpio_info.iomux_name, pdata->headset_gpio_info.iomux_mode);
525                 if (ret) 
526                         goto failed_free;       
527
528                 headset->irq[HEADSET] = gpio_to_irq(pdata->Headset_gpio);
529
530                 if(pdata->headset_in_type == HEADSET_IN_HIGH)
531                         headset->irq_type[HEADSET] = IRQF_TRIGGER_RISING;
532                 else
533                         headset->irq_type[HEADSET] = IRQF_TRIGGER_FALLING;
534                 ret = request_irq(headset->irq[HEADSET], headset_interrupt, headset->irq_type[HEADSET], "headset_input", NULL);
535                 if (ret) 
536                         goto failed_free_dev;
537                 enable_irq_wake(headset->irq[HEADSET]);
538         }
539         else
540                 goto failed_free_dev;
541 //------------------------------------------------------------------
542         if (pdata->Hook_gpio) {
543                 ret = pdata->hook_io_init(pdata->Hook_gpio, pdata->hook_gpio_info.iomux_name, pdata->hook_gpio_info.iomux_mode);
544                 if (ret) 
545                         goto failed_free;
546                 headset->irq[HOOK] = gpio_to_irq(pdata->Hook_gpio);
547                 headset->irq_type[HOOK] = IRQF_TRIGGER_FALLING;
548         
549                 ret = request_irq(headset->irq[HOOK], Hook_interrupt, headset->irq_type[HOOK] , "headset_hook", NULL);
550                 if (ret) 
551                         goto failed_free_dev;
552                 disable_irq(headset->irq[HOOK]);
553         }
554 //------------------------------------------------------------------    
555
556         return 0;       
557         
558 failed_free_dev:
559         platform_set_drvdata(pdev, NULL);
560         input_free_device(headset->input_dev);
561 failed_free:
562         dev_err(&pdev->dev, "failed to headset probe\n");
563         kfree(headset);
564         return ret;
565 }
566
567 static int rockchip_headsetobserve_suspend(struct platform_device *pdev, pm_message_t state)
568 {
569         DBG("%s----%d\n",__FUNCTION__,__LINE__);
570         disable_irq(headset_info->irq[HEADSET]);
571         disable_irq(headset_info->irq[HOOK]);
572
573         return 0;
574 }
575
576 static int rockchip_headsetobserve_resume(struct platform_device *pdev)
577 {
578         DBG("%s----%d\n",__FUNCTION__,__LINE__);        
579         enable_irq(headset_info->irq[HEADSET]);
580         enable_irq(headset_info->irq[HOOK]);
581         
582         return 0;
583 }
584
585 static struct platform_driver rockchip_headsetobserve_driver = {
586         .probe  = rockchip_headsetobserve_probe,
587 //      .resume =       rockchip_headsetobserve_resume, 
588 //      .suspend =      rockchip_headsetobserve_suspend,        
589         .driver = {
590                 .name   = "rk_headsetdet",
591                 .owner  = THIS_MODULE,
592         },
593 };
594
595 static int __init rockchip_headsetobserve_init(void)
596 {
597         platform_driver_register(&rockchip_headsetobserve_driver);
598         return 0;
599 }
600 module_init(rockchip_headsetobserve_init);
601 MODULE_DESCRIPTION("Rockchip Headset Driver");
602 MODULE_LICENSE("GPL");