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