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