Merge tag 'lsk-android-14.05' into develop-3.10
[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/device.h>
18 #include <linux/fs.h>
19 #include <linux/interrupt.h>
20 #include <linux/workqueue.h>
21 #include <linux/irq.h>
22 #include <linux/delay.h>
23 #include <linux/types.h>
24 #include <linux/input.h>
25 #include <linux/platform_device.h>
26 #include <linux/mutex.h>
27 #include <linux/errno.h>
28 #include <linux/err.h>
29 #include <linux/hrtimer.h>
30 #include <linux/switch.h>
31 #include <linux/input.h>
32 #include <linux/debugfs.h>
33 #include <linux/wakelock.h>
34 #include <asm/gpio.h>
35 #include <asm/atomic.h>
36 #include <asm/mach-types.h>
37 #include <linux/pm.h>
38 #include <linux/i2c.h>
39 #include <linux/spi/spi.h>
40 #include "rk_headset.h"
41 #ifdef CONFIG_HAS_EARLYSUSPEND
42 #include <linux/earlysuspend.h>
43 #endif
44
45 /* Debug */
46 #if 0
47 #define DBG(x...) printk(x)
48 #else
49 #define DBG(x...) do { } while (0)
50 #endif
51
52 #define BIT_HEADSET             (1 << 0)
53 #define BIT_HEADSET_NO_MIC      (1 << 1)
54
55 #define HEADSET 0
56 #define HOOK 1
57
58 #define HEADSET_IN 1
59 #define HEADSET_OUT 0
60 #define HOOK_DOWN 1
61 #define HOOK_UP 0
62 #define enable 1
63 #define disable 0
64
65 #if defined(CONFIG_SND_RK_SOC_RK2928) || defined(CONFIG_SND_RK29_SOC_RK610)
66 extern void rk2928_codec_set_spk(bool on);
67 #endif
68 #ifdef CONFIG_SND_SOC_WM8994
69 extern int wm8994_set_status(void);
70 #endif
71
72 /* headset private data */
73 struct headset_priv {
74         struct input_dev *input_dev;
75         struct rk_headset_pdata *pdata;
76         unsigned int headset_status:1;
77         unsigned int hook_status:1;
78         unsigned int isMic:1;
79         unsigned int isHook_irq:1;
80         int cur_headset_status; 
81         
82         unsigned int irq[2];
83         unsigned int irq_type[2];
84         struct delayed_work h_delayed_work[2];
85         struct switch_dev sdev;
86         struct mutex mutex_lock[2];     
87         struct timer_list headset_timer;
88         unsigned char *keycodes;
89 };
90 static struct headset_priv *headset_info;
91
92 #ifdef CONFIG_MODEM_MIC_SWITCH
93 #define HP_MIC 0
94 #define MAIN_MIC 1
95 void Modem_Mic_switch(int value)
96 {
97         if(value == HP_MIC)
98                 gpio_set_value(headset_info->pdata->mic_switch_gpio, headset_info->pdata->hp_mic_io_value);
99         else if(value == MAIN_MIC)
100                 gpio_set_value(headset_info->pdata->mic_switch_gpio,headset_info->pdata->main_mic_io_value);
101 }
102 void Modem_Mic_release(void)
103 {
104         if(headset_info->cur_headset_status == 1)
105                 gpio_set_value(headset_info->pdata->mic_switch_gpio, headset_info->pdata->hp_mic_io_value);
106         else
107                 gpio_set_value(headset_info->pdata->mic_switch_gpio,headset_info->pdata->main_mic_io_value);
108 }
109 #endif
110
111 static int read_gpio(int gpio)
112 {
113         int i,level;
114         for(i=0; i<3; i++)
115         {
116                 level = gpio_get_value(gpio);
117                 if(level < 0)
118                 {
119                         printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,gpio,i);
120                         msleep(1);
121                         continue;
122                 }
123                 else
124                 break;
125         }
126         if(level < 0)
127                 printk("%s:get pin level  err!\n",__FUNCTION__);
128
129         return level;
130 }
131
132 static irqreturn_t headset_interrupt(int irq, void *dev_id)
133 {
134         DBG("---headset_interrupt---\n");       
135         schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(50));
136         return IRQ_HANDLED;
137 }
138
139 static irqreturn_t hook_interrupt(int irq, void *dev_id)
140 {
141         DBG("---Hook_interrupt---\n");  
142 //      disable_irq_nosync(headset_info->irq[HOOK]);
143         schedule_delayed_work(&headset_info->h_delayed_work[HOOK], msecs_to_jiffies(100));
144         return IRQ_HANDLED;
145 }
146
147 static void headsetobserve_work(struct work_struct *work)
148 {
149         int level = 0;
150         int level2 = 0;
151         struct rk_headset_pdata *pdata = headset_info->pdata;
152         static unsigned int old_status = 0;
153         DBG("---headsetobserve_work---\n");
154         mutex_lock(&headset_info->mutex_lock[HEADSET]);
155
156         level = read_gpio(pdata->headset_gpio);
157         if(level < 0)
158                 goto out;
159         msleep(100);    
160         level2 = read_gpio(pdata->headset_gpio);
161         if(level2 < 0)
162                 goto out;
163         if(level2 != level)
164                 goto out;
165         old_status = headset_info->headset_status;
166         if(pdata->headset_insert_type == HEADSET_IN_HIGH)
167                 headset_info->headset_status = level?HEADSET_IN:HEADSET_OUT;
168         else
169                 headset_info->headset_status = level?HEADSET_OUT:HEADSET_IN;
170
171         if(old_status == headset_info->headset_status)  {
172                 DBG("old_status == headset_info->headset_status\n");
173                 goto out;
174         }
175         
176         DBG("(headset in is %s)headset status is %s\n",
177                 pdata->headset_insert_type?"high level":"low level",
178                 headset_info->headset_status?"in":"out");
179                 
180         if(headset_info->headset_status == HEADSET_IN)
181         {
182                 headset_info->cur_headset_status = BIT_HEADSET_NO_MIC;
183                 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
184                         irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_FALLING);
185                 else
186                         irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_RISING);
187                 if (pdata->hook_gpio) {
188                         del_timer(&headset_info->headset_timer);//Start the timer, wait for switch to the headphone channel
189                         headset_info->headset_timer.expires = jiffies + 100;
190                         add_timer(&headset_info->headset_timer);
191                         goto out;
192                 }
193         }
194         else if(headset_info->headset_status == HEADSET_OUT)
195         {       
196                 headset_info->hook_status = HOOK_UP;
197                 if(headset_info->isHook_irq == enable)
198                 {
199                         DBG("disable headset_hook irq\n");
200                         headset_info->isHook_irq = disable;
201                         disable_irq(headset_info->irq[HOOK]);           
202                 }       
203                 headset_info->cur_headset_status = 0;//~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
204                 //#if defined(CONFIG_SND_RK_SOC_RK2928) || defined(CONFIG_SOC_RK3028)
205                 //rk2928_codec_set_spk(HEADSET_OUT);
206                 //#endif                                                
207                 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
208                         irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_RISING);
209                 else
210                         irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_FALLING);
211         }
212 //      rk28_send_wakeup_key();
213         switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);        
214         #if defined(CONFIG_SND_RK_SOC_RK2928) || defined(CONFIG_SND_RK29_SOC_RK610)
215         if (headset_info->headset_status == HEADSET_OUT)
216         {
217                 mdelay(200);
218                 rk2928_codec_set_spk(HEADSET_OUT);
219                 gpio_set_value(pdata->Sw_mic_gpio, headset_info->pdata->Main_mic_io_value);
220         }
221         #endif
222         DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);
223
224 out:
225         mutex_unlock(&headset_info->mutex_lock[HEADSET]);       
226 }
227
228 static void hook_work(struct work_struct *work)
229 {
230         int level = 0;
231         struct rk_headset_pdata *pdata = headset_info->pdata;
232         static unsigned int old_status = HOOK_UP;
233
234         mutex_lock(&headset_info->mutex_lock[HOOK]);
235         
236         if(headset_info->headset_status == HEADSET_OUT){
237                 DBG("Headset is out\n");
238                 goto RE_ERROR;
239         }
240         
241         #ifdef CONFIG_SND_SOC_WM8994
242         if(wm8994_set_status() != 0)    {
243                 DBG("wm8994 is not set on heatset channel or suspend\n");
244                 goto RE_ERROR;
245         }
246         #endif
247         
248         level = read_gpio(pdata->hook_gpio);
249         if(level < 0)
250                 goto RE_ERROR;
251         
252         old_status = headset_info->hook_status;
253         DBG("Hook_work -- level = %d\n",level);
254         
255         if(level == 0)
256                 headset_info->hook_status = pdata->hook_down_type == HOOK_DOWN_HIGH?HOOK_UP:HOOK_DOWN;
257         else if(level > 0)      
258                 headset_info->hook_status = pdata->hook_down_type == HOOK_DOWN_HIGH?HOOK_DOWN:HOOK_UP;
259                 
260         if(old_status == headset_info->hook_status)
261         {
262                 DBG("old_status == headset_info->hook_status\n");
263                 goto RE_ERROR;
264         }       
265         DBG("Hook_work -- level = %d  hook status is %s\n",level,headset_info->hook_status?"key down":"key up");        
266         if(headset_info->hook_status == HOOK_DOWN)
267         {
268                 if(pdata->hook_down_type == HOOK_DOWN_HIGH)
269                         irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_FALLING);
270                 else
271                         irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_RISING);          
272         }
273         else
274         {
275                 if(pdata->hook_down_type == HOOK_DOWN_HIGH)
276                         irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_RISING);
277                 else
278                         irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_FALLING);
279         }
280         input_report_key(headset_info->input_dev,HOOK_KEY_CODE,headset_info->hook_status);
281         input_sync(headset_info->input_dev);    
282 RE_ERROR:
283         mutex_unlock(&headset_info->mutex_lock[HOOK]);
284 }
285
286 static void headset_timer_callback(unsigned long arg)
287 {
288         struct headset_priv *headset = (struct headset_priv *)(arg);
289         struct rk_headset_pdata *pdata = headset->pdata;
290         int level = 0;
291         
292 //      printk("headset_timer_callback,headset->headset_status=%d\n",headset->headset_status);  
293
294         if(headset->headset_status == HEADSET_OUT)
295         {
296                 printk("Headset is out\n");
297                 goto out;
298         }
299         #ifdef CONFIG_SND_SOC_WM8994
300         if(wm8994_set_status() != 0)
301         {
302         //      printk("wait wm8994 set the MICB2\n");
303         //      headset_info->headset_timer.expires = jiffies + 500;
304                 headset_info->headset_timer.expires = jiffies + 10;
305                 add_timer(&headset_info->headset_timer);        
306                 goto out;
307         }
308         #endif
309         level = read_gpio(pdata->hook_gpio);
310         if(level < 0)
311                 goto out;
312         if((level > 0 && pdata->hook_down_type == HOOK_DOWN_LOW)
313                 || (level == 0 && pdata->hook_down_type == HOOK_DOWN_HIGH))
314         {
315                 headset->isMic = 1;//have mic
316                 DBG("enable headset_hook irq\n");
317                 enable_irq(headset_info->irq[HOOK]);
318                 headset->isHook_irq = enable;
319                 headset_info->hook_status = HOOK_UP;
320                 if(pdata->hook_down_type == HOOK_DOWN_HIGH)
321                         irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_RISING);
322                 else
323                         irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_FALLING);
324
325         }       
326         else    
327                 headset->isMic= 0;//No microphone
328                 
329         printk("headset->isMic = %d\n",headset->isMic); 
330         headset_info->cur_headset_status = headset_info->isMic ? 1:2;//BIT_HEADSET:BIT_HEADSET_NO_MIC;//
331         #if defined(CONFIG_SND_RK_SOC_RK2928) || defined(CONFIG_SND_RK29_SOC_RK610)
332         rk2928_codec_set_spk(HEADSET_IN);
333         if(headset_info->cur_headset_status == 1)
334                 gpio_set_value(pdata->Sw_mic_gpio, pdata->Hp_mic_io_value);
335         #endif
336 //      rk28_send_wakeup_key();
337         switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
338         
339         DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);        
340
341 out:
342         return;
343 }
344
345 static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
346 {
347         return sprintf(buf, "Headset\n");
348 }
349
350 #ifdef CONFIG_HAS_EARLYSUSPEND
351 static void headset_early_resume(struct early_suspend *h)
352 {
353         schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(10));
354         //DBG(">>>>>headset_early_resume\n");
355 }
356
357 static struct early_suspend hs_early_suspend;
358 #endif
359
360 static int rk_hskey_open(struct input_dev *dev)
361 {
362         //struct rk28_adckey *adckey = input_get_drvdata(dev);
363 //      DBG("===========rk_Hskey_open===========\n");
364         return 0;
365 }
366
367 static void rk_hskey_close(struct input_dev *dev)
368 {
369 //      DBG("===========rk_Hskey_close===========\n");
370 //      struct rk28_adckey *adckey = input_get_drvdata(dev);
371
372 }
373
374 int rk_headset_probe(struct platform_device *pdev,struct rk_headset_pdata *pdata)
375 {
376         int ret;
377         struct headset_priv *headset;
378         
379         headset = kzalloc(sizeof(struct headset_priv), GFP_KERNEL);
380         if (headset == NULL) {
381                 dev_err(&pdev->dev, "failed to allocate driver data\n");
382                 return -ENOMEM;
383         }
384
385         headset->pdata = pdata;
386         headset->headset_status = HEADSET_OUT;
387         headset->hook_status = HOOK_UP;
388         headset->isHook_irq = disable;
389         headset->cur_headset_status = 0;
390         headset->sdev.name = "h2w";
391         headset->sdev.print_name = h2w_print_name;
392         ret = switch_dev_register(&headset->sdev);
393         if (ret < 0)
394                 goto failed_free;
395         
396         mutex_init(&headset->mutex_lock[HEADSET]);
397         mutex_init(&headset->mutex_lock[HOOK]);
398         
399         INIT_DELAYED_WORK(&headset->h_delayed_work[HEADSET], headsetobserve_work);
400         INIT_DELAYED_WORK(&headset->h_delayed_work[HOOK], hook_work);
401
402         headset->isMic = 0;
403         setup_timer(&headset->headset_timer, headset_timer_callback, (unsigned long)headset);
404 //------------------------------------------------------------------    
405         // Create and register the input driver. 
406         headset->input_dev = input_allocate_device();
407         if (!headset->input_dev) {
408                 dev_err(&pdev->dev, "failed to allocate input device\n");
409                 ret = -ENOMEM;
410                 goto failed_free;
411         }       
412         headset->input_dev->name = pdev->name;
413         headset->input_dev->open = rk_hskey_open;
414         headset->input_dev->close = rk_hskey_close;
415         headset->input_dev->dev.parent = &pdev->dev;
416         //input_dev->phys = KEY_PHYS_NAME;
417         headset->input_dev->id.vendor = 0x0001;
418         headset->input_dev->id.product = 0x0001;
419         headset->input_dev->id.version = 0x0100;
420         // Register the input device 
421         ret = input_register_device(headset->input_dev);
422         if (ret) {
423                 dev_err(&pdev->dev, "failed to register input device\n");
424                 goto failed_free_dev;
425         }
426         input_set_capability(headset->input_dev, EV_KEY,HOOK_KEY_CODE);
427
428 #ifdef CONFIG_HAS_EARLYSUSPEND
429         hs_early_suspend.suspend = NULL;
430         hs_early_suspend.resume = headset_early_resume;
431         hs_early_suspend.level = ~0x0;
432         register_early_suspend(&hs_early_suspend);
433 #endif
434         //------------------------------------------------------------------
435         if (pdata->headset_gpio) {
436                 if(!pdata->headset_gpio){
437                         dev_err(&pdev->dev,"failed init headset,please full hook_io_init function in board\n");
438                         goto failed_free_dev;
439                 }       
440
441                 headset->irq[HEADSET] = gpio_to_irq(pdata->headset_gpio);
442
443                 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
444                         headset->irq_type[HEADSET] = IRQF_TRIGGER_RISING;
445                 else
446                         headset->irq_type[HEADSET] = IRQF_TRIGGER_FALLING;
447                 ret = request_irq(headset->irq[HEADSET], headset_interrupt, headset->irq_type[HEADSET], "headset_input", NULL);
448                 if (ret) 
449                         goto failed_free_dev;
450                 enable_irq_wake(headset->irq[HEADSET]);
451         }
452         else
453                 goto failed_free_dev;
454 //------------------------------------------------------------------
455         if (pdata->hook_gpio) {
456                 headset->irq[HOOK] = gpio_to_irq(pdata->hook_gpio);
457                 headset->irq_type[HOOK] = pdata->hook_down_type == HOOK_DOWN_HIGH ? IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
458                         
459                 ret = request_irq(headset->irq[HOOK], hook_interrupt, headset->irq_type[HOOK] , "headset_hook", NULL);
460                 if (ret) 
461                         goto failed_free_dev;
462                 disable_irq(headset->irq[HOOK]);
463         }
464 //------------------------------------------------------------------    
465         headset_info = headset;
466         schedule_delayed_work(&headset->h_delayed_work[HEADSET], msecs_to_jiffies(500));                
467         return 0;       
468         
469 failed_free_dev:
470         platform_set_drvdata(pdev, NULL);
471         input_free_device(headset->input_dev);
472 failed_free:
473         dev_err(&pdev->dev, "failed to headset probe\n");
474         kfree(headset);
475         return ret;
476 }
477
478
479