1 /* arch/arm/mach-rockchip/rk28_headset.c
3 * Copyright (C) 2009 Rockchip Corporation.
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.
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.
16 #include <linux/module.h>
17 #include <linux/sysdev.h>
18 #include <linux/device.h>
20 #include <linux/interrupt.h>
21 #include <linux/workqueue.h>
22 #include <linux/irq.h>
23 #include <linux/delay.h>
24 #include <linux/types.h>
25 #include <linux/input.h>
26 #include <linux/platform_device.h>
27 #include <linux/mutex.h>
28 #include <linux/errno.h>
29 #include <linux/err.h>
30 #include <linux/hrtimer.h>
31 #include <linux/switch.h>
32 #include <linux/input.h>
33 #include <linux/debugfs.h>
34 #include <linux/wakelock.h>
36 #include <asm/atomic.h>
37 #include <asm/mach-types.h>
38 #include "rk_headset.h"
39 #include <linux/earlysuspend.h>
40 #include <linux/gpio.h>
41 #include <mach/board.h>
42 #include <linux/slab.h>
43 #include <linux/adc.h>
44 #include <linux/wakelock.h>
48 #define DBG(x...) printk(x)
50 #define DBG(x...) do { } while (0)
53 #define HOOK_ADC_SAMPLE_TIME 50
54 #define HOOK_LEVEL_HIGH 410 //1V*1024/2.5
55 #define HOOK_LEVEL_LOW 204 //0.5V*1024/2.5
56 #define HOOK_DEFAULT_VAL 1024
58 #define BIT_HEADSET (1 << 0)
59 #define BIT_HEADSET_NO_MIC (1 << 1)
71 #define HEADSET_TIMER 1
78 #ifdef CONFIG_SND_SOC_WM8994
79 extern int wm8994_headset_mic_detect(bool headset_status);
81 #ifdef CONFIG_SND_SOC_RT5631_PHONE
82 extern int rt5631_headset_mic_detect(bool headset_status);
84 #if defined (CONFIG_SND_SOC_RT3261) || defined (CONFIG_SND_SOC_RT3224)
85 extern int rt3261_headset_mic_detect(int jack_insert);
88 /* headset private data */
90 struct input_dev *input_dev;
91 struct rk_headset_pdata *pdata;
92 unsigned int headset_status:1;
93 unsigned int hook_status:1;
95 unsigned int heatset_irq_working;// headset interrupt working will not check hook key
96 int cur_headset_status;
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 struct timer_list headset_timer;
104 unsigned char *keycodes;
105 struct adc_client *client;
106 struct timer_list hook_timer;
107 unsigned int hook_time;//ms
108 struct wake_lock headset_on_wake;
110 static struct headset_priv *headset_info;
112 int Headset_isMic(void)
114 return headset_info->isMic;
116 EXPORT_SYMBOL_GPL(Headset_isMic);
119 static irqreturn_t headset_interrupt(int irq, void *dev_id)
121 struct rk_headset_pdata *pdata = headset_info->pdata;
122 static unsigned int old_status = 0;
125 wake_lock(&headset_info->headset_on_wake);
126 if(headset_info->heatset_irq_working == BUSY || headset_info->heatset_irq_working == WAIT)
128 DBG("In the headset_interrupt for read headset level wake_lock headset_on_wake\n");
129 headset_info->heatset_irq_working = BUSY;
133 level = gpio_get_value(pdata->Headset_gpio);
136 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Headset_gpio,i);
145 printk("%s:get pin level err!\n",__FUNCTION__);
149 old_status = headset_info->headset_status;
150 switch(pdata->headset_in_type)
152 case HEADSET_IN_HIGH:
154 headset_info->headset_status = HEADSET_IN;
156 headset_info->headset_status = HEADSET_OUT;
160 headset_info->headset_status = HEADSET_IN;
162 headset_info->headset_status = HEADSET_OUT;
165 DBG("---- ERROR: on headset headset_in_type error -----\n");
168 if(old_status == headset_info->headset_status)
170 DBG("Read Headset IO level old status == now status\n");
174 DBG("(headset in is %s)headset status is %s\n",
175 pdata->headset_in_type?"high level":"low level",
176 headset_info->headset_status?"in":"out");
177 if(headset_info->headset_status == HEADSET_IN)
182 if(adc_sync_read(headset_info->client) > HOOK_DEFAULT_VAL
183 || adc_sync_read(headset_info->client) < 0)
185 printk("headset is showly inside\n");
191 if(pdata->headset_in_type == HEADSET_IN_HIGH)
192 old_status = headset_info->headset_status = gpio_get_value(pdata->Headset_gpio)?HEADSET_IN:HEADSET_OUT;
194 old_status = headset_info->headset_status = gpio_get_value(pdata->Headset_gpio)?HEADSET_OUT:HEADSET_IN;
195 if(headset_info->headset_status == HEADSET_OUT)
200 if(pdata->Hook_adc_chn>=0 && 3>=pdata->Hook_adc_chn)
202 // wait for find Hook key
203 //#ifdef CONFIG_SND_SOC_RT5625
205 //headset_info->isMic = rt5625_headset_mic_detect(true);
206 #ifdef CONFIG_SND_SOC_WM8994
207 wm8994_headset_mic_detect(true);
209 #if defined (CONFIG_SND_SOC_RT3261) || defined (CONFIG_SND_SOC_RT3224)
210 rt3261_headset_mic_detect(true);
212 #ifdef CONFIG_SND_SOC_RT5631_PHONE
213 rt5631_headset_mic_detect(true);
216 adc_value = adc_sync_read(headset_info->client);
217 if(adc_value >= 0 && adc_value < HOOK_LEVEL_LOW)
219 headset_info->isMic= 0;//No microphone
220 #ifdef CONFIG_SND_SOC_WM8994
221 wm8994_headset_mic_detect(false);
223 #if defined (CONFIG_SND_SOC_RT3261) || defined (CONFIG_SND_SOC_RT3224)
224 rt3261_headset_mic_detect(false);
226 #ifdef CONFIG_SND_SOC_RT5631_PHONE
227 rt5631_headset_mic_detect(false);
230 else if(adc_value >= HOOK_LEVEL_HIGH)
231 headset_info->isMic = 1;//have mic
233 if(headset_info->isMic < 0)
235 printk("codec is error\n");
236 headset_info->heatset_irq_working = WAIT;
237 if(pdata->headset_in_type == HEADSET_IN_HIGH)
238 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_LOW|IRQF_ONESHOT);
240 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_HIGH|IRQF_ONESHOT);
241 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(0));
242 wake_unlock(&headset_info->headset_on_wake);
245 //adc_value = adc_sync_read(headset_info->client);
246 printk("headset adc value = %d\n",adc_value);
247 if(headset_info->isMic) {
248 if(adc_value > HOOK_DEFAULT_VAL || adc_value < HOOK_LEVEL_HIGH)
250 mod_timer(&headset_info->hook_timer, jiffies + msecs_to_jiffies(1000));
253 headset_info->cur_headset_status = headset_info->isMic ? BIT_HEADSET:BIT_HEADSET_NO_MIC;
257 headset_info->isMic= 0;//No microphone
258 headset_info->cur_headset_status = BIT_HEADSET_NO_MIC;
260 printk("headset->isMic = %d\n",headset_info->isMic);
261 if(pdata->headset_in_type == HEADSET_IN_HIGH)
262 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_FALLING);
264 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_RISING);
266 else if(headset_info->headset_status == HEADSET_OUT)
268 headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
269 del_timer(&headset_info->hook_timer);
270 if(headset_info->isMic)
272 headset_info->hook_status = HOOK_UP;
273 #ifdef CONFIG_SND_SOC_WM8994
274 //rt5625_headset_mic_detect(false);
275 wm8994_headset_mic_detect(false);
277 #if defined (CONFIG_SND_SOC_RT3261) || defined (CONFIG_SND_SOC_RT3224)
278 rt3261_headset_mic_detect(false);
280 #ifdef CONFIG_SND_SOC_RT5631_PHONE
281 rt5631_headset_mic_detect(false);
284 if(pdata->headset_in_type == HEADSET_IN_HIGH)
285 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_RISING);
287 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_FALLING);
290 rk28_send_wakeup_key();
291 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
292 DBG("headset notice android headset status = %d\n",headset_info->cur_headset_status);
294 // schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(0));
296 headset_info->heatset_irq_working = IDLE;
297 wake_unlock(&headset_info->headset_on_wake);
301 static int headset_change_irqtype(int type,unsigned int irq_type)
304 free_irq(headset_info->irq[type],NULL);
306 DBG("%s: type is %s irqtype is %s\n",__FUNCTION__, type?"hook":"headset",(irq_type == IRQF_TRIGGER_RISING)?"RISING":"FALLING");
307 // DBG("%s: type is %s irqtype is %s\n",__FUNCTION__, type?"hook":"headset",(irq_type == IRQF_TRIGGER_LOW)?"LOW":"HIGH");
311 ret = request_threaded_irq(headset_info->irq[type],NULL, headset_interrupt, irq_type, "headset_input", NULL);
313 DBG("headset_change_irqtype: request irq failed\n");
322 static void headsetobserve_work(struct work_struct *work)
324 struct rk_headset_pdata *pdata = headset_info->pdata;
325 DBG("In the headsetobserve_work headset_status is %s\n",headset_info->headset_status?"in":"out");
327 if(headset_info->heatset_irq_working == WAIT && headset_info->headset_status == HEADSET_IN)
329 printk("wait for codec\n");
330 headset_info->heatset_irq_working = IDLE;
331 headset_info->headset_status = HEADSET_OUT;
333 free_irq(headset_info->irq[HEADSET],NULL);
335 if(pdata->headset_in_type == HEADSET_IN_HIGH)
336 headset_info->irq_type[HEADSET] = IRQF_TRIGGER_HIGH|IRQF_ONESHOT;
338 headset_info->irq_type[HEADSET] = IRQF_TRIGGER_LOW|IRQF_ONESHOT;
339 if(request_threaded_irq(headset_info->irq[HEADSET], NULL,headset_interrupt, headset_info->irq_type[HEADSET]|IRQF_NO_SUSPEND, "headset_input", NULL) < 0)
340 printk("headset request_threaded_irq error\n");
344 if(pdata->headset_in_type == HEADSET_IN_HIGH && headset_info->headset_status == HEADSET_IN)
345 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);
346 else if(pdata->headset_in_type == HEADSET_IN_LOW && headset_info->headset_status == HEADSET_IN)
347 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);
349 if(pdata->headset_in_type == HEADSET_IN_HIGH && headset_info->headset_status == HEADSET_OUT)
350 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);
351 else if(pdata->headset_in_type == HEADSET_IN_LOW && headset_info->headset_status == HEADSET_OUT)
352 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);
356 static void hook_adc_callback(struct adc_client *client, void *client_param, int result)
359 struct headset_priv *headset = (struct headset_priv *)client_param;
360 struct rk_headset_pdata *pdata = headset->pdata;
361 static unsigned int old_status = HOOK_UP;
363 // DBG("hook_adc_callback read adc value: %d\n",level);
367 printk("%s:get adc level err = %d!\n",__FUNCTION__,level);
371 if(headset->headset_status == HEADSET_OUT
372 || headset->heatset_irq_working == BUSY
373 || headset->heatset_irq_working == WAIT
374 || pdata->headset_in_type?gpio_get_value(pdata->Headset_gpio) == 0:gpio_get_value(pdata->Headset_gpio) > 0)
376 DBG("Headset is out or waiting for headset is in or out,after same time check HOOK key\n");
380 old_status = headset->hook_status;
381 if(level < HOOK_LEVEL_LOW && level >= 0)
382 headset->hook_status = HOOK_DOWN;
383 else if(level > HOOK_LEVEL_HIGH && level < HOOK_DEFAULT_VAL)
384 headset->hook_status = HOOK_UP;
386 // DBG("hook_adc_callback read adc value.........outside showly....: %d\n",level);
387 del_timer(&headset->hook_timer);
388 mod_timer(&headset->hook_timer, jiffies + msecs_to_jiffies(50));
392 if(old_status == headset->hook_status)
394 // DBG("Hook adc read old_status == headset->hook_status hook_time = %d\n",headset->hook_time);
398 DBG("HOOK status is %s , adc value = %d hook_time = %d\n",headset->hook_status?"down":"up",level,headset->hook_time);
399 if(headset->headset_status == HEADSET_OUT
400 || headset->heatset_irq_working == BUSY
401 || headset->heatset_irq_working == WAIT
402 || (pdata->headset_in_type?gpio_get_value(pdata->Headset_gpio) == 0:gpio_get_value(pdata->Headset_gpio) > 0))
403 DBG("headset is out,HOOK status must discard\n");
406 input_report_key(headset->input_dev,pdata->hook_key_code,headset->hook_status);
407 input_sync(headset->input_dev);
411 static void hook_timer_callback(unsigned long arg)
413 struct headset_priv *headset = (struct headset_priv *)(arg);
414 // DBG("hook_timer_callback\n");
415 if(headset->headset_status == HEADSET_OUT
416 || headset->heatset_irq_working == BUSY
417 || headset->heatset_irq_working == WAIT)
419 adc_async_read(headset->client);
420 mod_timer(&headset->hook_timer, jiffies + msecs_to_jiffies(headset->hook_time));
423 static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
425 return sprintf(buf, "Headset\n");
428 #ifdef CONFIG_HAS_EARLYSUSPEND
429 static void headset_early_resume(struct early_suspend *h)
431 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(10));
432 //DBG(">>>>>headset_early_resume\n");
435 static struct early_suspend hs_early_suspend;
438 static int rk_Hskey_open(struct input_dev *dev)
440 //struct rk28_adckey *adckey = input_get_drvdata(dev);
441 // DBG("===========rk_Hskey_open===========\n");
445 static void rk_Hskey_close(struct input_dev *dev)
447 // DBG("===========rk_Hskey_close===========\n");
448 // struct rk28_adckey *adckey = input_get_drvdata(dev);
452 static int rockchip_headsetobserve_probe(struct platform_device *pdev)
455 struct headset_priv *headset;
456 struct rk_headset_pdata *pdata;
458 headset = kzalloc(sizeof(struct headset_priv), GFP_KERNEL);
459 if (headset == NULL) {
460 dev_err(&pdev->dev, "failed to allocate driver data\n");
463 headset_info = headset;
464 headset->pdata = pdev->dev.platform_data;
465 pdata = headset->pdata;
466 headset->headset_status = HEADSET_OUT;
467 headset->heatset_irq_working = IDLE;
468 headset->hook_status = HOOK_UP;
469 headset->hook_time = HOOK_ADC_SAMPLE_TIME;
470 headset->cur_headset_status = 0;
471 headset->sdev.name = "h2w";
472 headset->sdev.print_name = h2w_print_name;
473 ret = switch_dev_register(&headset->sdev);
477 // mutex_init(&headset->mutex_lock[HEADSET]);
478 // mutex_init(&headset->mutex_lock[HOOK]);
479 wake_lock_init(&headset->headset_on_wake, WAKE_LOCK_SUSPEND, "headset_on_wake");
480 INIT_DELAYED_WORK(&headset->h_delayed_work[HEADSET], headsetobserve_work);
483 //------------------------------------------------------------------
484 // Create and register the input driver.
485 headset->input_dev = input_allocate_device();
486 if (!headset->input_dev) {
487 dev_err(&pdev->dev, "failed to allocate input device\n");
491 headset->input_dev->name = pdev->name;
492 headset->input_dev->open = rk_Hskey_open;
493 headset->input_dev->close = rk_Hskey_close;
494 headset->input_dev->dev.parent = &pdev->dev;
495 //input_dev->phys = KEY_PHYS_NAME;
496 headset->input_dev->id.vendor = 0x0001;
497 headset->input_dev->id.product = 0x0001;
498 headset->input_dev->id.version = 0x0100;
499 // Register the input device
500 ret = input_register_device(headset->input_dev);
502 dev_err(&pdev->dev, "failed to register input device\n");
503 goto failed_free_dev;
506 input_set_capability(headset->input_dev, EV_KEY, pdata->hook_key_code);
507 //------------------------------------------------------------------
508 if (pdata->Headset_gpio) {
509 if(pdata->Headset_gpio == NULL){
510 dev_err(&pdev->dev,"failed init headset,please full hook_io_init function in board\n");
511 goto failed_free_dev;
513 ret = pdata->headset_io_init(pdata->Headset_gpio);
515 goto failed_free_dev;
517 headset->irq[HEADSET] = gpio_to_irq(pdata->Headset_gpio);
519 if(pdata->headset_in_type == HEADSET_IN_HIGH)
520 headset->irq_type[HEADSET] = IRQF_TRIGGER_HIGH|IRQF_ONESHOT;
522 headset->irq_type[HEADSET] = IRQF_TRIGGER_LOW|IRQF_ONESHOT;
523 ret = request_threaded_irq(headset->irq[HEADSET], NULL,headset_interrupt, headset->irq_type[HEADSET]|IRQF_NO_SUSPEND, "headset_input", NULL);
525 goto failed_free_dev;
526 enable_irq_wake(headset->irq[HEADSET]);
529 goto failed_free_dev;
530 //------------------------------------------------------------------
531 if(pdata->Hook_adc_chn>=0 && 3>=pdata->Hook_adc_chn)
533 headset->client = adc_register(pdata->Hook_adc_chn, hook_adc_callback, (void *)headset);
534 if(!headset->client) {
535 printk("hook adc register error\n");
537 goto failed_free_dev;
539 setup_timer(&headset->hook_timer,hook_timer_callback, (unsigned long)headset);
540 printk("headset adc default value = %d\n",adc_sync_read(headset->client));
543 #ifdef CONFIG_HAS_EARLYSUSPEND
544 hs_early_suspend.suspend = NULL;
545 hs_early_suspend.resume = headset_early_resume;
546 hs_early_suspend.level = ~0x0;
547 register_early_suspend(&hs_early_suspend);
553 platform_set_drvdata(pdev, NULL);
554 input_free_device(headset->input_dev);
556 dev_err(&pdev->dev, "failed to headset probe\n");
561 static int rockchip_headsetobserve_suspend(struct platform_device *pdev, pm_message_t state)
563 DBG("%s----%d\n",__FUNCTION__,__LINE__);
564 // disable_irq(headset_info->irq[HEADSET]);
565 del_timer(&headset_info->hook_timer);
569 static int rockchip_headsetobserve_resume(struct platform_device *pdev)
571 DBG("%s----%d\n",__FUNCTION__,__LINE__);
572 // enable_irq(headset_info->irq[HEADSET]);
573 if(headset_info->isMic)
574 mod_timer(&headset_info->hook_timer, jiffies + msecs_to_jiffies(1500));
578 static struct platform_driver rockchip_headsetobserve_driver = {
579 .probe = rockchip_headsetobserve_probe,
580 .resume = rockchip_headsetobserve_resume,
581 .suspend = rockchip_headsetobserve_suspend,
583 .name = "rk_headsetdet",
584 .owner = THIS_MODULE,
588 static int __init rockchip_headsetobserve_init(void)
590 platform_driver_register(&rockchip_headsetobserve_driver);
593 late_initcall(rockchip_headsetobserve_init);
594 MODULE_DESCRIPTION("Rockchip Headset Driver");
595 MODULE_LICENSE("GPL");