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