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