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