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