headset: detect errory
[firefly-linux-kernel-4.4.55.git] / drivers / headset_observe / rk_headset_irq_hook_adc.c
1 /* 
2  * Copyright (C) 2009 Rockchip Corporation.
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  */
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/fs.h>
17 #include <linux/interrupt.h>
18 #include <linux/workqueue.h>
19 #include <linux/irq.h>
20 #include <linux/delay.h>
21 #include <linux/types.h>
22 #include <linux/input.h>
23 #include <linux/platform_device.h>
24 #include <linux/mutex.h>
25 #include <linux/errno.h>
26 #include <linux/err.h>
27 #include <linux/hrtimer.h>
28 #include <linux/switch.h>
29 #include <linux/input.h>
30 #include <linux/debugfs.h>
31 #include <linux/wakelock.h>
32 #include <linux/pm.h>
33 #include <linux/i2c.h>
34 #include <linux/spi/spi.h>
35 #include <linux/iio/consumer.h>
36 #include <linux/adc.h>
37 #include <linux/wakelock.h>
38
39 #include <asm/gpio.h>
40 #include <asm/atomic.h>
41 #include <asm/mach-types.h>
42
43 #include "rk_headset.h"
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 HOOK_ADC_SAMPLE_TIME    100
53 #define HOOK_LEVEL_HIGH                 410             //1V*1024/2.5
54 #define HOOK_LEVEL_LOW                  204             //0.5V*1024/2.5
55 #define HOOK_DEFAULT_VAL                1024    
56
57 #define BIT_HEADSET             (1 << 0)
58 #define BIT_HEADSET_NO_MIC      (1 << 1)
59
60 #define HEADSET 0
61 #define HOOK 1
62
63 #define HEADSET_IN 1
64 #define HEADSET_OUT 0
65 #define HOOK_DOWN 1
66 #define HOOK_UP 0
67 #define enable 1
68 #define disable 0
69
70 #define HEADSET_TIMER 1
71 #define HOOK_TIMER 2
72
73 #define WAIT 2
74 #define BUSY 1
75 #define IDLE 0
76
77 #ifdef CONFIG_SND_SOC_WM8994
78 extern int wm8994_headset_mic_detect(bool headset_status);
79 #endif
80 #ifdef CONFIG_SND_SOC_RT5631_PHONE
81 extern int rt5631_headset_mic_detect(bool headset_status);
82 #endif
83 #if defined (CONFIG_SND_SOC_RT3261) || defined (CONFIG_SND_SOC_RT3224)
84 extern int rt3261_headset_mic_detect(int jack_insert);
85 #endif
86
87 /* headset private data */
88 struct headset_priv {
89         struct input_dev *input_dev;
90         struct rk_headset_pdata *pdata;
91         unsigned int headset_status:1;
92         unsigned int hook_status:1;
93         int isMic;
94         struct iio_channel *chan;
95         unsigned int heatset_irq_working;// headset interrupt working will not check hook key   
96         int cur_headset_status; 
97         
98         unsigned int irq[2];
99         unsigned int irq_type[2];
100         struct delayed_work h_delayed_work[2];
101         struct switch_dev sdev;
102         struct mutex mutex_lock[2];     
103         unsigned char *keycodes;
104         struct delayed_work hook_work;
105         unsigned int hook_time;//ms
106 };
107 static struct headset_priv *headset_info;
108
109 //1
110 static irqreturn_t headset_interrupt(int irq, void *dev_id)
111 {
112         struct rk_headset_pdata *pdata = headset_info->pdata;
113         static unsigned int old_status = 0;
114         int i,level = 0;        
115         
116         disable_irq_nosync(headset_info->irq[HEADSET]);
117         if(headset_info->heatset_irq_working == BUSY || headset_info->heatset_irq_working == WAIT)
118                 return IRQ_HANDLED;
119
120         DBG("In the headset_interrupt\n");              
121         headset_info->heatset_irq_working = BUSY;
122         msleep(150);
123         for(i=0; i<3; i++)
124         {
125                 level = gpio_get_value(pdata->headset_gpio);
126                 if(level < 0)
127                 {
128                         printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->headset_gpio,i);
129                         msleep(1);
130                         continue;
131                 }
132                 else
133                 break;
134         }
135         if(level < 0)
136         {
137                 printk("%s:get pin level  err!\n",__FUNCTION__);
138                 goto out;
139         }
140         else
141                 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->headset_gpio,i);
142
143         old_status = headset_info->headset_status;
144         switch(pdata->headset_insert_type)
145         {
146         case HEADSET_IN_HIGH:
147                 if(level > 0)
148                         headset_info->headset_status = HEADSET_IN;
149                 else if(level == 0)
150                         headset_info->headset_status = HEADSET_OUT;     
151                 break;
152         case HEADSET_IN_LOW:
153                 if(level == 0)
154                         headset_info->headset_status = HEADSET_IN;
155                 else if(level > 0)
156                         headset_info->headset_status = HEADSET_OUT;             
157                 break;                  
158         default:
159                 DBG("---- ERROR: on headset headset_insert_type error -----\n");
160                 break;                  
161         }
162         if(old_status == headset_info->headset_status)
163         {
164                 DBG("Read Headset IO level old status == now status =%d\n",headset_info->headset_status);
165                 goto out;
166         }
167
168         DBG("(headset in is %s)headset status is %s\n",
169                 pdata->headset_insert_type?"high level":"low level",
170                 headset_info->headset_status?"in":"out");
171         if(headset_info->headset_status == HEADSET_IN)
172         {
173                 if(pdata->chan != 0)
174                 {
175                         //detect Hook key
176                         schedule_delayed_work(&headset_info->h_delayed_work[HOOK],msecs_to_jiffies(200));
177                 }
178                 else
179                 {
180                         headset_info->isMic= 0;//No microphone
181                         headset_info->cur_headset_status = BIT_HEADSET_NO_MIC;
182
183                         switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);        
184                         DBG("headset notice android headset status = %d\n",headset_info->cur_headset_status);
185                 }
186
187                 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
188                         irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_FALLING);
189                 else
190                         irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_RISING);
191         }
192         else if(headset_info->headset_status == HEADSET_OUT)
193         {
194                 headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
195                 cancel_delayed_work(&headset_info->hook_work);
196                 if(headset_info->isMic)
197                 {
198                         headset_info->hook_status = HOOK_UP;
199                         #ifdef CONFIG_SND_SOC_WM8994
200                         //rt5625_headset_mic_detect(false);
201                         wm8994_headset_mic_detect(false);
202                         #endif
203                         #if defined (CONFIG_SND_SOC_RT3261) || defined (CONFIG_SND_SOC_RT3224)
204                         rt3261_headset_mic_detect(false);
205                         #endif
206                         #ifdef CONFIG_SND_SOC_RT5631_PHONE
207                         rt5631_headset_mic_detect(false);
208                         #endif                          
209                 }       
210
211                 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
212                         irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_RISING);
213                 else
214                         irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_FALLING);      
215
216                 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);        
217                 DBG("headset notice android headset status = %d\n",headset_info->cur_headset_status);           
218         }       
219 //      rk_send_wakeup_key();   
220 out:
221         headset_info->heatset_irq_working = IDLE;
222         enable_irq(headset_info->irq[HEADSET]);
223         return IRQ_HANDLED;
224 }
225
226 static int headset_change_irqtype(int type,unsigned int irq_type)
227 {
228         int ret = 0;
229         free_irq(headset_info->irq[type],NULL);
230
231         DBG("%s: type is %s irqtype is %s\n",__FUNCTION__,      type?"hook":"headset",(irq_type == IRQF_TRIGGER_RISING)?"RISING":"FALLING");
232 //      DBG("%s: type is %s irqtype is %s\n",__FUNCTION__,      type?"hook":"headset",(irq_type == IRQF_TRIGGER_LOW)?"LOW":"HIGH");
233         switch(type)
234         {
235         case HEADSET:
236                 ret = request_threaded_irq(headset_info->irq[type],NULL, headset_interrupt, irq_type, "headset_input", NULL);
237                 if (ret<0) 
238                         DBG("headset_change_irqtype: request irq failed\n");            
239                 break;
240         default:
241                 ret = -1;
242                 break;
243         }
244         return ret;
245 }
246
247 static void hook_once_work(struct work_struct *work)
248 {
249         int ret,val;
250
251         #ifdef CONFIG_SND_SOC_WM8994
252         wm8994_headset_mic_detect(true);
253         #endif
254
255         #if defined (CONFIG_SND_SOC_RT3261) || defined (CONFIG_SND_SOC_RT3224)
256         rt3261_headset_mic_detect(true);
257         #endif
258
259         #ifdef CONFIG_SND_SOC_RT5631_PHONE
260         rt5631_headset_mic_detect(true);
261         #endif
262
263         ret = iio_read_channel_raw(headset_info->chan, &val);
264         if (ret < 0) {
265                 pr_err("read hook_once_work adc channel() error: %d\n", ret);
266         }
267         else
268                 DBG("hook_once_work read adc value: %d\n",val);
269
270         if(val >= 0 && val < HOOK_LEVEL_LOW)
271         {
272                 headset_info->isMic= 0;//No microphone
273                 #ifdef CONFIG_SND_SOC_WM8994
274                 wm8994_headset_mic_detect(false);
275                 #endif
276
277                 #if defined (CONFIG_SND_SOC_RT3261) || defined (CONFIG_SND_SOC_RT3224)
278                 rt3261_headset_mic_detect(false);
279                 #endif  
280
281                 #ifdef CONFIG_SND_SOC_RT5631_PHONE
282                 rt5631_headset_mic_detect(false);
283                 #endif                                  
284         }       
285         else if(val >= HOOK_LEVEL_HIGH)
286         {
287                 headset_info->isMic = 1;//have mic
288                 schedule_delayed_work(&headset_info->hook_work,msecs_to_jiffies(100));
289         }
290
291         headset_info->cur_headset_status = headset_info->isMic ? BIT_HEADSET:BIT_HEADSET_NO_MIC;
292         
293         switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);        
294         DBG("%s notice android headset status = %d\n",__func__,headset_info->cur_headset_status);
295 }
296
297 //2
298 static void headsetobserve_work(struct work_struct *work)
299 {
300         struct rk_headset_pdata *pdata = headset_info->pdata;
301
302         DBG("In the headsetobserve_work headset_status is %s\n",headset_info->headset_status?"in":"out");
303
304         if(headset_info->heatset_irq_working == WAIT && headset_info->headset_status == HEADSET_IN)
305         {
306                 printk("wait for codec\n");
307                 headset_info->heatset_irq_working = IDLE;
308                 headset_info->headset_status = HEADSET_OUT;     
309                 
310                 free_irq(headset_info->irq[HEADSET],NULL);      
311                 msleep(100);
312                 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
313                         headset_info->irq_type[HEADSET] = IRQF_TRIGGER_LOW|IRQF_ONESHOT;
314                 else
315                         headset_info->irq_type[HEADSET] = IRQF_TRIGGER_HIGH|IRQF_ONESHOT;
316                 if(request_threaded_irq(headset_info->irq[HEADSET], NULL,headset_interrupt, headset_info->irq_type[HEADSET]|IRQF_NO_SUSPEND, "headset_input", NULL) < 0)
317                         printk("headset request_threaded_irq error\n");
318                 return; 
319         }
320 /*      
321         if(pdata->headset_insert_type == HEADSET_IN_HIGH && headset_info->headset_status == HEADSET_IN)
322                 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);
323         else if(pdata->headset_insert_type == HEADSET_IN_LOW && headset_info->headset_status == HEADSET_IN)
324                 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);
325
326         if(pdata->headset_insert_type == HEADSET_IN_HIGH && headset_info->headset_status == HEADSET_OUT)
327                 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);
328         else if(pdata->headset_insert_type == HEADSET_IN_LOW && headset_info->headset_status == HEADSET_OUT)
329                 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);
330 */              
331 }
332 //4
333 static void hook_work_callback(struct work_struct *work)
334 {
335         int ret,val;
336         struct headset_priv *headset = headset_info;
337         struct rk_headset_pdata *pdata = headset->pdata;
338         static unsigned int old_status = HOOK_UP;
339
340
341         ret = iio_read_channel_raw(headset->chan, &val);
342         if (ret < 0) {
343                 pr_err("read hook adc channel() error: %d\n", ret);
344                 goto out;
345         }
346         else
347                 DBG("hook_work_callback read adc value=%d\n",val);
348
349         if(headset->headset_status == HEADSET_OUT
350                 || headset->heatset_irq_working == BUSY
351                 || headset->heatset_irq_working == WAIT
352                 || pdata->headset_insert_type?gpio_get_value(pdata->headset_gpio) == 0:gpio_get_value(pdata->headset_gpio) > 0)
353         {
354                 DBG("Headset is out or waiting for headset is in or out,after same time check HOOK key\n");
355                 goto out;
356         }
357
358         old_status = headset->hook_status;
359         if(val < HOOK_LEVEL_LOW && val >= 0)    
360                 headset->hook_status = HOOK_DOWN;
361         else if(val > HOOK_LEVEL_HIGH && val < HOOK_DEFAULT_VAL)
362                 headset->hook_status = HOOK_UP;
363         
364         DBG("HOOK status is %s , adc value = %d hook_time = %d\n",headset->hook_status?"down":"up",val,headset->hook_time);
365
366         if(old_status == headset->hook_status)
367         {
368                 DBG("Hook adc read old_status == headset->hook_status=%d hook_time = %d\n",headset->hook_status,headset->hook_time);
369                 goto status_error;
370         }       
371                 
372         if(headset->headset_status == HEADSET_OUT
373                 || headset->heatset_irq_working == BUSY
374                 || headset->heatset_irq_working == WAIT
375                 || (pdata->headset_insert_type?gpio_get_value(pdata->headset_gpio) == 0:gpio_get_value(pdata->headset_gpio) > 0))
376         {
377                 printk("headset is out,HOOK status must discard\n");
378                 goto out;
379         }
380         else
381         {
382                 input_report_key(headset->input_dev,HOOK_KEY_CODE,headset->hook_status);
383                 input_sync(headset->input_dev);
384         }       
385 status_error:
386          schedule_delayed_work(&headset_info->hook_work,msecs_to_jiffies(100));
387 out:;
388 }
389
390 static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
391 {
392         return sprintf(buf, "Headset\n");
393 }
394
395
396 static int rk_hskey_open(struct input_dev *dev)
397 {
398         return 0;
399 }
400
401 static void rk_hskey_close(struct input_dev *dev)
402 {
403
404 }
405
406 int rk_headset_adc_probe(struct platform_device *pdev,struct rk_headset_pdata *pdata)
407 {
408         int ret;
409         struct headset_priv *headset;
410
411         headset =devm_kzalloc(&pdev->dev,sizeof(struct headset_priv), GFP_KERNEL);
412         if (headset == NULL) {
413                 dev_err(&pdev->dev, "failed to allocate driver data\n");
414                 return -ENOMEM;
415         }
416         headset_info = headset;
417         headset->pdata = pdata;
418         headset->headset_status = HEADSET_OUT;
419         headset->heatset_irq_working = IDLE;
420         headset->hook_status = HOOK_UP;
421         headset->hook_time = HOOK_ADC_SAMPLE_TIME;
422         headset->cur_headset_status = 0;
423         headset->sdev.name = "h2w";
424         headset->sdev.print_name = h2w_print_name;
425         ret = switch_dev_register(&headset->sdev);
426         if (ret < 0)
427                 goto failed_free;
428         
429 //      mutex_init(&headset->mutex_lock[HEADSET]);
430 //      mutex_init(&headset->mutex_lock[HOOK]);
431         INIT_DELAYED_WORK(&headset->h_delayed_work[HEADSET], headsetobserve_work);
432         INIT_DELAYED_WORK(&headset->h_delayed_work[HOOK], hook_once_work);
433
434         headset->isMic = 0;
435 //------------------------------------------------------------------            
436         // Create and register the input driver. 
437         headset->input_dev = input_allocate_device();
438         if (!headset->input_dev) {
439                 dev_err(&pdev->dev, "failed to allocate input device\n");
440                 ret = -ENOMEM;
441                 goto failed_free;
442         }       
443         headset->input_dev->name = pdev->name;
444         headset->input_dev->open = rk_hskey_open;
445         headset->input_dev->close = rk_hskey_close;
446         headset->input_dev->dev.parent = &pdev->dev;
447         //input_dev->phys = KEY_PHYS_NAME;
448         headset->input_dev->id.vendor = 0x0001;
449         headset->input_dev->id.product = 0x0001;
450         headset->input_dev->id.version = 0x0100;
451         // Register the input device 
452         ret = input_register_device(headset->input_dev);
453         if (ret) {
454                 dev_err(&pdev->dev, "failed to register input device\n");
455                 goto failed_free_dev;
456         }
457
458         input_set_capability(headset->input_dev, EV_KEY, HOOK_KEY_CODE);
459 //------------------------------------------------------------------
460         if (pdata->headset_gpio) {
461                 if(!pdata->headset_gpio){
462                         dev_err(&pdev->dev,"failed init headset,please full hook_io_init function in board\n");
463                         goto failed_free_dev;
464                 }
465
466                 headset->irq[HEADSET] = gpio_to_irq(pdata->headset_gpio);
467
468                 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
469                         headset->irq_type[HEADSET] = IRQF_TRIGGER_HIGH|IRQF_ONESHOT;
470                 else
471                         headset->irq_type[HEADSET] = IRQF_TRIGGER_LOW|IRQF_ONESHOT;
472                 ret = request_threaded_irq(headset->irq[HEADSET], NULL,headset_interrupt, headset->irq_type[HEADSET]|IRQF_NO_SUSPEND, "headset_input", NULL);
473                 if (ret) 
474                         goto failed_free_dev;
475                 enable_irq_wake(headset->irq[HEADSET]);
476         }
477         else
478                 goto failed_free_dev;
479 //------------------------------------------------------------------
480         if(pdata->chan != NULL)
481         {
482                 headset->chan = pdata->chan;
483                 INIT_DELAYED_WORK(&headset->hook_work, hook_work_callback);
484         }
485
486         return 0;       
487         
488 failed_free_dev:
489         platform_set_drvdata(pdev, NULL);
490         input_free_device(headset->input_dev);
491 failed_free:
492         dev_err(&pdev->dev, "failed to headset probe\n");
493         return ret;
494 }
495
496 int rk_headset_adc_suspend(struct platform_device *pdev, pm_message_t state)
497 {
498         DBG("%s----%d\n",__FUNCTION__,__LINE__);
499 //      disable_irq(headset_info->irq[HEADSET]);
500 //      del_timer(&headset_info->hook_timer);
501         return 0;
502 }
503
504 int rk_headset_adc_resume(struct platform_device *pdev)
505 {
506         DBG("%s----%d\n",__FUNCTION__,__LINE__);        
507 //      enable_irq(headset_info->irq[HEADSET]);
508 //      if(headset_info->isMic)
509 //              mod_timer(&headset_info->hook_timer, jiffies + msecs_to_jiffies(1500)); 
510         return 0;
511 }
512
513