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