Merge branch 'develop-3.0' of ssh://192.168.1.29/rk/kernel into develop-3.0
[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 1
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, NULL, NULL);
120                 break;
121         case HEADSET:
122                 ret = request_irq(headset_info->irq[type], headset_interrupt, irq_type, NULL, 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                 //      enable_irq(headset_info->irq[HOOK]);
195                         headset_info->cur_headset_status = BIT_HEADSET;
196                         headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);//
197                         if (pdata->Headset_gpio != NULL) {
198                                 del_timer(&headset_info->headset_timer);//Start the timer, wait for switch to the headphone channel
199                                 headset_info->headset_timer.expires = jiffies + 500;
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_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                 }
215                 break;
216         case HEADSET_IN_LOW:
217                 if(level == 0)
218                 {//in--High level
219                 //      DBG("---HEADSET_IN_LOW headset in LOW ---\n");
220                         headset_info->cur_headset_status = BIT_HEADSET;
221                         headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);//
222                         if (pdata->Headset_gpio != NULL) {                      
223                                 del_timer(&headset_info->headset_timer);//Start the timer, wait for switch to the headphone channel
224                                 headset_info->headset_timer.expires = jiffies + 500;
225                                 add_timer(&headset_info->headset_timer);
226                         }       
227                 }
228                 else if(level > 0)
229                 {//out--High level
230                 //      DBG("---HEADSET_IN_LOW headset out LOW ---\n");
231                         if(headset_info->isHook_irq == enable)
232                         {
233                         //      DBG("disable_irq\n");
234                                 headset_info->isHook_irq = disable;
235                                 disable_irq(headset_info->irq[HOOK]);           
236                         }                               
237                         headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
238                         headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);//
239                 }
240                 break;                  
241         default:
242                 DBG("---- ERROR: on headset headset_in_type error -----\n");
243                 break;                  
244         }
245         rk28_send_wakeup_key();
246         switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);        
247         DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);
248 RE_ERROR:
249         mutex_unlock(&headset_info->mutex_lock[HEADSET]);       
250 }
251
252 static void Hook_work(struct work_struct *work)
253 {
254         int i,level = 0;
255         struct rk_headset_pdata *pdata = headset_info->pdata;
256         static unsigned int old_status = HOOK_UP;
257
258 //      DBG("---Hook_work---\n");
259         mutex_lock(&headset_info->mutex_lock[HOOK]);
260         if(headset_info->headset_status == HEADSET_OUT)
261         {
262                 DBG("Headset is out\n");
263                 goto RE_ERROR;
264         }       
265         /*#ifdef CONFIG_SND_SOC_WM8994
266         if(wm8994_set_status() < 0)
267         {
268                 DBG("wm8994 is not set on heatset channel or suspend\n");
269                 goto RE_ERROR;
270         }
271         #endif*/
272         for(i=0; i<3; i++)
273         {
274                 level = gpio_get_value(pdata->Hook_gpio);
275                 if(level < 0)
276                 {
277                         printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Hook_gpio,i);
278                         msleep(1);
279                         continue;
280                 }
281                 else
282                 break;
283         }
284         if(level < 0)
285         {
286                 printk("%s:get pin level  err!\n",__FUNCTION__);
287                 goto RE_ERROR;
288         }
289         
290         old_status = headset_info->hook_status;
291         if(level == 0)
292                 headset_info->hook_status = HOOK_UP;
293         else if(level > 0)      
294                 headset_info->hook_status = HOOK_DOWN;
295         if(old_status == headset_info->hook_status)
296         {
297                 DBG("old_status == headset_info->hook_status\n");
298                 goto RE_ERROR;
299         }       
300         
301         if(level == 0)
302         {
303                 DBG("---HOOK Down ---\n");
304                 headset_change_irqtype(HOOK,IRQF_TRIGGER_RISING);//
305                 input_report_key(headset_info->input_dev,pdata->hook_key_code,headset_info->hook_status);
306                 input_sync(headset_info->input_dev);
307         }
308         else if(level > 0)
309         {
310                 DBG("---HOOK Up ---\n");                
311                 headset_change_irqtype(HOOK,IRQF_TRIGGER_FALLING);//
312                 input_report_key(headset_info->input_dev,pdata->hook_key_code,headset_info->hook_status);
313                 input_sync(headset_info->input_dev);
314         }
315 RE_ERROR:
316         mutex_unlock(&headset_info->mutex_lock[HOOK]);
317 }
318
319 static void headset_timer_callback(unsigned long arg)
320 {
321         struct headset_priv *headset = (struct headset_priv *)(arg);
322         struct rk_headset_pdata *pdata = headset->pdata;
323         int i,level = 0;
324         
325         DBG("headset_timer_callback,headset->headset_status=%d\n",headset->headset_status);     
326
327         if(headset->headset_status == HEADSET_OUT)
328         {
329                 DBG("Headset is out\n");
330                 goto out;
331         }
332         /*#ifdef CONFIG_SND_SOC_WM8994
333         if(wm8994_set_status() < 0)
334         {
335                 DBG("wm8994 is not set on heatset channel\n");
336                 headset_info->headset_timer.expires = jiffies + 500;
337                 add_timer(&headset_info->headset_timer);        
338                 goto out;
339         }
340         #endif*/
341         for(i=0; i<3; i++)
342         {
343                 level = gpio_get_value(pdata->Hook_gpio);
344                 if(level < 0)
345                 {
346                         printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Hook_gpio,i);
347                         msleep(1);
348                         continue;
349                 }
350                 else
351                 break;
352         }
353         if(level < 0)
354         {
355                 printk("%s:get pin level  err!\n",__FUNCTION__);
356                 goto out;
357         }
358
359         if(level == 0)
360                 headset->isMic= 0;//No microphone
361         else if(level > 0)      
362         {       
363                 headset->isMic = 1;//have mic
364         //      DBG("enable_irq\n");    
365                 enable_irq(headset_info->irq[HOOK]);
366                 headset->isHook_irq = enable;
367         }       
368         DBG("headset->isMic = %d\n",headset->isMic);
369 out:
370         return;
371 }
372
373 static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
374 {
375         return sprintf(buf, "Headset\n");
376 }
377
378 #ifdef CONFIG_HAS_EARLYSUSPEND
379 static void headset_early_resume(struct early_suspend *h)
380 {
381         schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(10));
382         //DBG(">>>>>headset_early_resume\n");
383 }
384
385 static struct early_suspend hs_early_suspend;
386 #endif
387
388 static int rk_Hskey_open(struct input_dev *dev)
389 {
390         //struct rk28_adckey *adckey = input_get_drvdata(dev);
391 //      DBG("===========rk_Hskey_open===========\n");
392         return 0;
393 }
394
395 static void rk_Hskey_close(struct input_dev *dev)
396 {
397 //      DBG("===========rk_Hskey_close===========\n");
398 //      struct rk28_adckey *adckey = input_get_drvdata(dev);
399
400 }
401
402 static int rockchip_headsetobserve_probe(struct platform_device *pdev)
403 {
404         int ret;
405         struct headset_priv *headset;
406         struct rk_headset_pdata *pdata;
407         
408         headset = kzalloc(sizeof(struct headset_priv), GFP_KERNEL);
409         if (headset == NULL) {
410                 dev_err(&pdev->dev, "failed to allocate driver data\n");
411                 return -ENOMEM;
412         }       
413         headset->pdata = pdev->dev.platform_data;
414         pdata = headset->pdata;
415         headset->headset_status = HEADSET_OUT;
416         headset->hook_status = HOOK_UP;
417         headset->isHook_irq = disable;
418         headset->cur_headset_status = 0;
419         headset->sdev.name = "h2w";
420         headset->sdev.print_name = h2w_print_name;
421         ret = switch_dev_register(&headset->sdev);
422         if (ret < 0)
423                 goto failed_free;
424         
425         mutex_init(&headset->mutex_lock[HEADSET]);
426         mutex_init(&headset->mutex_lock[HOOK]);
427         
428         INIT_DELAYED_WORK(&headset->h_delayed_work[HEADSET], headsetobserve_work);
429         INIT_DELAYED_WORK(&headset->h_delayed_work[HOOK], Hook_work);
430
431 //      init_timer(&headset->headset_timer);
432 //      headset->headset_timer.function = headset_timer_callback;
433 //      headset->headset_timer.data = (unsigned long)headset;
434 //      headset->headset_timer.expires = jiffies + 3000;
435         headset->isMic = 0;
436         setup_timer(&headset->headset_timer, headset_timer_callback, (unsigned long)headset);
437 //      headset->headset_timer.expires = jiffies + 1000;
438 //      add_timer(&headset->headset_timer);     
439 //------------------------------------------------------------------
440         if (pdata->Headset_gpio != NULL) {
441                 ret = gpio_request(pdata->Headset_gpio, NULL);
442                 if (ret) 
443                         goto failed_free;
444                 gpio_pull_updown(pdata->Headset_gpio, PullDisable);
445                 gpio_direction_input(pdata->Headset_gpio);
446                 headset->irq[HEADSET] = gpio_to_irq(pdata->Headset_gpio);
447
448                 if(pdata->headset_in_type == HEADSET_IN_HIGH)
449                         headset->irq_type[HEADSET] = IRQF_TRIGGER_RISING;
450                 else
451                         headset->irq_type[HEADSET] = IRQF_TRIGGER_FALLING;
452                 ret = request_irq(headset->irq[HEADSET], headset_interrupt, headset->irq_type[HEADSET], "headset_input", NULL);
453                 if (ret) 
454                         goto failed_free;
455                 enable_irq_wake(headset->irq[HEADSET]);
456         }
457 //------------------------------------------------------------------
458         if (pdata->Hook_gpio != NULL) {
459                 ret = gpio_request(pdata->Hook_gpio , NULL);
460                 if (ret) 
461                         goto failed_free;
462                 gpio_pull_updown(pdata->Hook_gpio, PullDisable);
463                 gpio_direction_input(pdata->Hook_gpio);
464                 headset->irq[HOOK] = gpio_to_irq(pdata->Hook_gpio);
465                 headset->irq_type[HOOK] = IRQF_TRIGGER_FALLING;
466         
467                 ret = request_irq(headset->irq[HOOK], Hook_interrupt, headset->irq_type[HOOK] , "headset_hook", NULL);
468                 if (ret) 
469                         goto failed_free;
470                 disable_irq(headset->irq[HOOK]);
471         }
472 //------------------------------------------------------------------            
473         // Create and register the input driver. 
474         headset->input_dev = input_allocate_device();
475         if (!headset->input_dev) {
476                 dev_err(&pdev->dev, "failed to allocate input device\n");
477                 ret = -ENOMEM;
478                 goto failed_free;
479         }       
480         headset->input_dev->name = pdev->name;
481         headset->input_dev->open = rk_Hskey_open;
482         headset->input_dev->close = rk_Hskey_close;
483         headset->input_dev->dev.parent = &pdev->dev;
484         //input_dev->phys = KEY_PHYS_NAME;
485         headset->input_dev->id.vendor = 0x0001;
486         headset->input_dev->id.product = 0x0001;
487         headset->input_dev->id.version = 0x0100;
488         // Register the input device 
489         ret = input_register_device(headset->input_dev);
490         if (ret) {
491                 dev_err(&pdev->dev, "failed to register input device\n");
492                 goto failed_free_dev;
493         }
494
495
496 //      headset->input_dev->keycode = headset->keycodes;
497 //      headset->input_dev->keycodesize = sizeof(unsigned char);
498 //      headset->input_dev->keycodemax = 2;
499         
500 //      set_bit(KEY_MEDIA, headset->input_dev->keybit);
501 //      clear_bit(0, headset->input_dev->keybit);
502         input_set_capability(headset->input_dev, EV_KEY, pdata->hook_key_code);
503 //      input_set_capability(headset->input_dev, EV_SW, SW_HEADPHONE_INSERT);
504 //      input_set_capability(headset->input_dev, EV_KEY, KEY_END);
505
506 //      headset->input_dev->evbit[0] = BIT_MASK(EV_KEY);
507         
508         headset_info = headset;
509         schedule_delayed_work(&headset->h_delayed_work[HEADSET], msecs_to_jiffies(500));        
510
511 #ifdef CONFIG_HAS_EARLYSUSPEND
512         hs_early_suspend.suspend = NULL;
513         hs_early_suspend.resume = headset_early_resume;
514         hs_early_suspend.level = ~0x0;
515         register_early_suspend(&hs_early_suspend);
516 #endif
517
518         return 0;       
519         
520 failed_free_dev:
521         platform_set_drvdata(pdev, NULL);
522         input_free_device(headset->input_dev);
523 failed_free:
524         kfree(headset); 
525         return ret;
526 }
527
528 static int rockchip_headsetobserve_suspend(struct platform_device *pdev, pm_message_t state)
529 {
530         DBG("%s----%d\n",__FUNCTION__,__LINE__);
531         disable_irq(headset_info->irq[HEADSET]);
532         disable_irq(headset_info->irq[HOOK]);
533
534         return 0;
535 }
536
537 static int rockchip_headsetobserve_resume(struct platform_device *pdev)
538 {
539         DBG("%s----%d\n",__FUNCTION__,__LINE__);        
540         enable_irq(headset_info->irq[HEADSET]);
541         enable_irq(headset_info->irq[HOOK]);
542         
543         return 0;
544 }
545
546 static struct platform_driver rockchip_headsetobserve_driver = {
547         .probe  = rockchip_headsetobserve_probe,
548 //      .resume =       rockchip_headsetobserve_resume, 
549 //      .suspend =      rockchip_headsetobserve_suspend,        
550         .driver = {
551                 .name   = "rk_headsetdet",
552                 .owner  = THIS_MODULE,
553         },
554 };
555
556 static int __init rockchip_headsetobserve_init(void)
557 {
558         platform_driver_register(&rockchip_headsetobserve_driver);
559         return 0;
560 }
561 module_init(rockchip_headsetobserve_init);
562 MODULE_DESCRIPTION("Rockchip Headset Driver");
563 MODULE_LICENSE("GPL");