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);
82 #if defined (CONFIG_SND_SOC_RT3261) || defined (CONFIG_SND_SOC_RT3224)
83 extern int rt3261_headset_mic_detect(int jack_insert);
86 /* headset private data */
88 struct input_dev *input_dev;
89 struct rk_headset_pdata *pdata;
90 unsigned int headset_status:1;
91 unsigned int hook_status:1;
93 unsigned int heatset_irq_working;// headset interrupt working will not check hook key
94 int cur_headset_status;
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 struct timer_list headset_timer;
102 unsigned char *keycodes;
103 struct adc_client *client;
104 struct timer_list hook_timer;
105 unsigned int hook_time;//ms
106 struct wake_lock headset_on_wake;
108 static struct headset_priv *headset_info;
110 int Headset_isMic(void)
112 return headset_info->isMic;
114 EXPORT_SYMBOL_GPL(Headset_isMic);
117 static irqreturn_t headset_interrupt(int irq, void *dev_id)
119 struct rk_headset_pdata *pdata = headset_info->pdata;
120 static unsigned int old_status = 0;
123 wake_lock(&headset_info->headset_on_wake);
124 if(headset_info->heatset_irq_working == BUSY || headset_info->heatset_irq_working == WAIT)
126 DBG("In the headset_interrupt for read headset level wake_lock headset_on_wake\n");
127 headset_info->heatset_irq_working = BUSY;
131 level = gpio_get_value(pdata->Headset_gpio);
134 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Headset_gpio,i);
143 printk("%s:get pin level err!\n",__FUNCTION__);
147 old_status = headset_info->headset_status;
148 switch(pdata->headset_in_type)
150 case HEADSET_IN_HIGH:
152 headset_info->headset_status = HEADSET_IN;
154 headset_info->headset_status = HEADSET_OUT;
158 headset_info->headset_status = HEADSET_IN;
160 headset_info->headset_status = HEADSET_OUT;
163 DBG("---- ERROR: on headset headset_in_type error -----\n");
166 if(old_status == headset_info->headset_status)
168 DBG("Read Headset IO level old status == now status\n");
172 DBG("(headset in is %s)headset status is %s\n",
173 pdata->headset_in_type?"high level":"low level",
174 headset_info->headset_status?"in":"out");
175 if(headset_info->headset_status == HEADSET_IN)
180 if(adc_sync_read(headset_info->client) > HOOK_DEFAULT_VAL
181 || adc_sync_read(headset_info->client) < 0)
183 printk("headset is showly inside\n");
189 if(pdata->headset_in_type == HEADSET_IN_HIGH)
190 old_status = headset_info->headset_status = gpio_get_value(pdata->Headset_gpio)?HEADSET_IN:HEADSET_OUT;
192 old_status = headset_info->headset_status = gpio_get_value(pdata->Headset_gpio)?HEADSET_OUT:HEADSET_IN;
193 if(headset_info->headset_status == HEADSET_OUT)
198 if(pdata->Hook_adc_chn>=0 && 3>=pdata->Hook_adc_chn)
200 // wait for find Hook key
201 //#ifdef CONFIG_SND_SOC_RT5625
203 //headset_info->isMic = rt5625_headset_mic_detect(true);
204 #ifdef CONFIG_SND_SOC_WM8994
205 wm8994_headset_mic_detect(true);
207 #if defined (CONFIG_SND_SOC_RT3261) || defined (CONFIG_SND_SOC_RT3224)
208 rt3261_headset_mic_detect(true);
211 adc_value = adc_sync_read(headset_info->client);
212 if(adc_value >= 0 && adc_value < HOOK_LEVEL_LOW)
214 headset_info->isMic= 0;//No microphone
215 #ifdef CONFIG_SND_SOC_WM8994
216 wm8994_headset_mic_detect(false);
218 #if defined (CONFIG_SND_SOC_RT3261) || defined (CONFIG_SND_SOC_RT3224)
219 rt3261_headset_mic_detect(false);
222 else if(adc_value >= HOOK_LEVEL_HIGH)
223 headset_info->isMic = 1;//have mic
225 if(headset_info->isMic < 0)
227 printk("codec is error\n");
228 headset_info->heatset_irq_working = WAIT;
229 if(pdata->headset_in_type == HEADSET_IN_HIGH)
230 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_LOW|IRQF_ONESHOT);
232 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_HIGH|IRQF_ONESHOT);
233 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(0));
234 wake_unlock(&headset_info->headset_on_wake);
237 //adc_value = adc_sync_read(headset_info->client);
238 printk("headset adc value = %d\n",adc_value);
239 if(headset_info->isMic) {
240 if(adc_value > HOOK_DEFAULT_VAL || adc_value < HOOK_LEVEL_HIGH)
242 mod_timer(&headset_info->hook_timer, jiffies + msecs_to_jiffies(1000));
245 headset_info->cur_headset_status = headset_info->isMic ? BIT_HEADSET:BIT_HEADSET_NO_MIC;
249 headset_info->isMic= 0;//No microphone
250 headset_info->cur_headset_status = BIT_HEADSET_NO_MIC;
252 printk("headset->isMic = %d\n",headset_info->isMic);
253 if(pdata->headset_in_type == HEADSET_IN_HIGH)
254 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_FALLING);
256 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_RISING);
258 else if(headset_info->headset_status == HEADSET_OUT)
260 headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
261 del_timer(&headset_info->hook_timer);
262 if(headset_info->isMic)
264 headset_info->hook_status = HOOK_UP;
265 #ifdef CONFIG_SND_SOC_WM8994
266 //rt5625_headset_mic_detect(false);
267 wm8994_headset_mic_detect(false);
269 #if defined (CONFIG_SND_SOC_RT3261) || defined (CONFIG_SND_SOC_RT3224)
270 rt3261_headset_mic_detect(false);
273 if(pdata->headset_in_type == HEADSET_IN_HIGH)
274 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_RISING);
276 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_FALLING);
279 rk28_send_wakeup_key();
280 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
281 DBG("headset notice android headset status = %d\n",headset_info->cur_headset_status);
283 // schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(0));
285 headset_info->heatset_irq_working = IDLE;
286 wake_unlock(&headset_info->headset_on_wake);
290 static int headset_change_irqtype(int type,unsigned int irq_type)
293 free_irq(headset_info->irq[type],NULL);
295 DBG("%s: type is %s irqtype is %s\n",__FUNCTION__, type?"hook":"headset",(irq_type == IRQF_TRIGGER_RISING)?"RISING":"FALLING");
296 // DBG("%s: type is %s irqtype is %s\n",__FUNCTION__, type?"hook":"headset",(irq_type == IRQF_TRIGGER_LOW)?"LOW":"HIGH");
300 ret = request_threaded_irq(headset_info->irq[type],NULL, headset_interrupt, irq_type, "headset_input", NULL);
302 DBG("headset_change_irqtype: request irq failed\n");
311 static void headsetobserve_work(struct work_struct *work)
313 struct rk_headset_pdata *pdata = headset_info->pdata;
314 DBG("In the headsetobserve_work headset_status is %s\n",headset_info->headset_status?"in":"out");
316 if(headset_info->heatset_irq_working == WAIT && headset_info->headset_status == HEADSET_IN)
318 printk("wait for codec\n");
319 headset_info->heatset_irq_working = IDLE;
320 headset_info->headset_status = HEADSET_OUT;
322 free_irq(headset_info->irq[HEADSET],NULL);
324 if(pdata->headset_in_type == HEADSET_IN_HIGH)
325 headset_info->irq_type[HEADSET] = IRQF_TRIGGER_HIGH|IRQF_ONESHOT;
327 headset_info->irq_type[HEADSET] = IRQF_TRIGGER_LOW|IRQF_ONESHOT;
328 if(request_threaded_irq(headset_info->irq[HEADSET], NULL,headset_interrupt, headset_info->irq_type[HEADSET]|IRQF_NO_SUSPEND, "headset_input", NULL) < 0)
329 printk("headset request_threaded_irq error\n");
333 if(pdata->headset_in_type == HEADSET_IN_HIGH && headset_info->headset_status == HEADSET_IN)
334 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);
335 else if(pdata->headset_in_type == HEADSET_IN_LOW && headset_info->headset_status == HEADSET_IN)
336 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);
338 if(pdata->headset_in_type == HEADSET_IN_HIGH && headset_info->headset_status == HEADSET_OUT)
339 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);
340 else if(pdata->headset_in_type == HEADSET_IN_LOW && headset_info->headset_status == HEADSET_OUT)
341 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);
345 static void hook_adc_callback(struct adc_client *client, void *client_param, int result)
348 struct headset_priv *headset = (struct headset_priv *)client_param;
349 struct rk_headset_pdata *pdata = headset->pdata;
350 static unsigned int old_status = HOOK_UP;
352 // DBG("hook_adc_callback read adc value: %d\n",level);
356 printk("%s:get adc level err = %d!\n",__FUNCTION__,level);
360 if(headset->headset_status == HEADSET_OUT
361 || headset->heatset_irq_working == BUSY
362 || headset->heatset_irq_working == WAIT
363 || pdata->headset_in_type?gpio_get_value(pdata->Headset_gpio) == 0:gpio_get_value(pdata->Headset_gpio) > 0)
365 DBG("Headset is out or waiting for headset is in or out,after same time check HOOK key\n");
369 old_status = headset->hook_status;
370 if(level < HOOK_LEVEL_LOW && level >= 0)
371 headset->hook_status = HOOK_DOWN;
372 else if(level > HOOK_LEVEL_HIGH && level < HOOK_DEFAULT_VAL)
373 headset->hook_status = HOOK_UP;
375 DBG("hook_adc_callback read adc value.........outside showly....: %d\n",level);
376 del_timer(&headset->hook_timer);
377 mod_timer(&headset->hook_timer, jiffies + msecs_to_jiffies(500));
381 if(old_status == headset->hook_status)
383 // DBG("Hook adc read old_status == headset->hook_status hook_time = %d\n",headset->hook_time);
387 DBG("HOOK status is %s , adc value = %d hook_time = %d\n",headset->hook_status?"down":"up",level,headset->hook_time);
388 if(headset->headset_status == HEADSET_OUT
389 || headset->heatset_irq_working == BUSY
390 || headset->heatset_irq_working == WAIT
391 || (pdata->headset_in_type?gpio_get_value(pdata->Headset_gpio) == 0:gpio_get_value(pdata->Headset_gpio) > 0))
392 DBG("headset is out,HOOK status must discard\n");
395 input_report_key(headset->input_dev,pdata->hook_key_code,headset->hook_status);
396 input_sync(headset->input_dev);
400 static void hook_timer_callback(unsigned long arg)
402 struct headset_priv *headset = (struct headset_priv *)(arg);
403 // DBG("hook_timer_callback\n");
404 if(headset->headset_status == HEADSET_OUT
405 || headset->heatset_irq_working == BUSY
406 || headset->heatset_irq_working == WAIT)
408 adc_async_read(headset->client);
409 mod_timer(&headset->hook_timer, jiffies + msecs_to_jiffies(headset->hook_time));
412 static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
414 return sprintf(buf, "Headset\n");
417 #ifdef CONFIG_HAS_EARLYSUSPEND
418 static void headset_early_resume(struct early_suspend *h)
420 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(10));
421 //DBG(">>>>>headset_early_resume\n");
424 static struct early_suspend hs_early_suspend;
427 static int rk_Hskey_open(struct input_dev *dev)
429 //struct rk28_adckey *adckey = input_get_drvdata(dev);
430 // DBG("===========rk_Hskey_open===========\n");
434 static void rk_Hskey_close(struct input_dev *dev)
436 // DBG("===========rk_Hskey_close===========\n");
437 // struct rk28_adckey *adckey = input_get_drvdata(dev);
441 static int rockchip_headsetobserve_probe(struct platform_device *pdev)
444 struct headset_priv *headset;
445 struct rk_headset_pdata *pdata;
447 headset = kzalloc(sizeof(struct headset_priv), GFP_KERNEL);
448 if (headset == NULL) {
449 dev_err(&pdev->dev, "failed to allocate driver data\n");
452 headset_info = headset;
453 headset->pdata = pdev->dev.platform_data;
454 pdata = headset->pdata;
455 headset->headset_status = HEADSET_OUT;
456 headset->heatset_irq_working = IDLE;
457 headset->hook_status = HOOK_UP;
458 headset->hook_time = HOOK_ADC_SAMPLE_TIME;
459 headset->cur_headset_status = 0;
460 headset->sdev.name = "h2w";
461 headset->sdev.print_name = h2w_print_name;
462 ret = switch_dev_register(&headset->sdev);
466 // mutex_init(&headset->mutex_lock[HEADSET]);
467 // mutex_init(&headset->mutex_lock[HOOK]);
468 wake_lock_init(&headset->headset_on_wake, WAKE_LOCK_SUSPEND, "headset_on_wake");
469 INIT_DELAYED_WORK(&headset->h_delayed_work[HEADSET], headsetobserve_work);
472 //------------------------------------------------------------------
473 // Create and register the input driver.
474 headset->input_dev = input_allocate_device();
475 if (!headset->input_dev) {
476 dev_err(&pdev->dev, "failed to allocate input device\n");
480 headset->input_dev->name = pdev->name;
481 headset->input_dev->open = rk_Hskey_open;
482 headset->input_dev->close = rk_Hskey_close;
483 headset->input_dev->dev.parent = &pdev->dev;
484 //input_dev->phys = KEY_PHYS_NAME;
485 headset->input_dev->id.vendor = 0x0001;
486 headset->input_dev->id.product = 0x0001;
487 headset->input_dev->id.version = 0x0100;
488 // Register the input device
489 ret = input_register_device(headset->input_dev);
491 dev_err(&pdev->dev, "failed to register input device\n");
492 goto failed_free_dev;
495 input_set_capability(headset->input_dev, EV_KEY, pdata->hook_key_code);
496 //------------------------------------------------------------------
497 if (pdata->Headset_gpio) {
498 ret = pdata->headset_io_init(pdata->Headset_gpio, pdata->headset_gpio_info.iomux_name, pdata->headset_gpio_info.iomux_mode);
502 headset->irq[HEADSET] = gpio_to_irq(pdata->Headset_gpio);
504 if(pdata->headset_in_type == HEADSET_IN_HIGH)
505 headset->irq_type[HEADSET] = IRQF_TRIGGER_HIGH|IRQF_ONESHOT;
507 headset->irq_type[HEADSET] = IRQF_TRIGGER_LOW|IRQF_ONESHOT;
508 ret = request_threaded_irq(headset->irq[HEADSET], NULL,headset_interrupt, headset->irq_type[HEADSET]|IRQF_NO_SUSPEND, "headset_input", NULL);
511 enable_irq_wake(headset->irq[HEADSET]);
515 //------------------------------------------------------------------
516 if(pdata->Hook_adc_chn>=0 && 3>=pdata->Hook_adc_chn)
518 headset->client = adc_register(pdata->Hook_adc_chn, hook_adc_callback, (void *)headset);
519 if(!headset->client) {
520 printk("hook adc register error\n");
524 setup_timer(&headset->hook_timer,hook_timer_callback, (unsigned long)headset);
525 printk("headset adc default value = %d\n",adc_sync_read(headset->client));
528 #ifdef CONFIG_HAS_EARLYSUSPEND
529 hs_early_suspend.suspend = NULL;
530 hs_early_suspend.resume = headset_early_resume;
531 hs_early_suspend.level = ~0x0;
532 register_early_suspend(&hs_early_suspend);
538 platform_set_drvdata(pdev, NULL);
539 input_free_device(headset->input_dev);
541 dev_err(&pdev->dev, "failed to headset probe\n");
546 static int rockchip_headsetobserve_suspend(struct platform_device *pdev, pm_message_t state)
548 DBG("%s----%d\n",__FUNCTION__,__LINE__);
549 // disable_irq(headset_info->irq[HEADSET]);
550 del_timer(&headset_info->hook_timer);
554 static int rockchip_headsetobserve_resume(struct platform_device *pdev)
556 DBG("%s----%d\n",__FUNCTION__,__LINE__);
557 // enable_irq(headset_info->irq[HEADSET]);
558 if(headset_info->isMic)
559 mod_timer(&headset_info->hook_timer, jiffies + msecs_to_jiffies(1500));
563 static struct platform_driver rockchip_headsetobserve_driver = {
564 .probe = rockchip_headsetobserve_probe,
565 .resume = rockchip_headsetobserve_resume,
566 .suspend = rockchip_headsetobserve_suspend,
568 .name = "rk_headsetdet",
569 .owner = THIS_MODULE,
573 static int __init rockchip_headsetobserve_init(void)
575 platform_driver_register(&rockchip_headsetobserve_driver);
578 late_initcall(rockchip_headsetobserve_init);
579 MODULE_DESCRIPTION("Rockchip Headset Driver");
580 MODULE_LICENSE("GPL");