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