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