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