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>
46 #define DBG(x...) printk(x)
48 #define DBG(x...) do { } while (0)
51 #define BIT_HEADSET (1 << 0)
52 #define BIT_HEADSET_NO_MIC (1 << 1)
64 #ifdef CONFIG_SND_RK_SOC_RK2928
65 extern void rk2928_codec_set_spk(bool on);
67 #ifdef CONFIG_SND_SOC_WM8994
68 extern int wm8994_set_status(void);
71 /* headset private data */
73 struct input_dev *input_dev;
74 struct rk_headset_pdata *pdata;
75 unsigned int headset_status:1;
76 unsigned int hook_status:1;
78 unsigned int isHook_irq:1;
79 int cur_headset_status;
82 unsigned int irq_type[2];
83 struct delayed_work h_delayed_work[2];
84 struct switch_dev sdev;
85 struct mutex mutex_lock[2];
86 struct timer_list headset_timer;
87 unsigned char *keycodes;
89 static struct headset_priv *headset_info;
91 int Headset_isMic(void)
93 return headset_info->isMic;
95 EXPORT_SYMBOL_GPL(Headset_isMic);
97 int Headset_status(void)
99 if(headset_info->cur_headset_status == BIT_HEADSET_NO_MIC ||
100 headset_info->cur_headset_status == BIT_HEADSET )
105 EXPORT_SYMBOL_GPL(Headset_status);
107 static irqreturn_t headset_interrupt(int irq, void *dev_id)
109 DBG("---headset_interrupt---\n");
110 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(50));
114 static irqreturn_t Hook_interrupt(int irq, void *dev_id)
116 DBG("---Hook_interrupt---\n");
117 // disable_irq_nosync(headset_info->irq[HOOK]);
118 schedule_delayed_work(&headset_info->h_delayed_work[HOOK], msecs_to_jiffies(100));
122 static int headset_change_irqtype(int type,unsigned int irq_type)
125 DBG("--------%s----------\n",__FUNCTION__);
126 free_irq(headset_info->irq[type],NULL);
131 ret = request_irq(headset_info->irq[type], Hook_interrupt, irq_type, "headset_input", NULL);
134 ret = request_irq(headset_info->irq[type], headset_interrupt, irq_type, "headset_hook", NULL);
143 DBG("headset_change_irqtype: request irq failed\n");
149 static void headsetobserve_work(struct work_struct *work)
152 struct rk_headset_pdata *pdata = headset_info->pdata;
153 static unsigned int old_status = 0;
154 DBG("---headsetobserve_work---\n");
155 mutex_lock(&headset_info->mutex_lock[HEADSET]);
159 level = gpio_get_value(pdata->Headset_gpio);
162 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Headset_gpio,i);
171 printk("%s:get pin level err!\n",__FUNCTION__);
175 old_status = headset_info->headset_status;
176 switch(pdata->headset_in_type)
178 case HEADSET_IN_HIGH:
180 headset_info->headset_status = HEADSET_IN;
182 headset_info->headset_status = HEADSET_OUT;
186 headset_info->headset_status = HEADSET_IN;
188 headset_info->headset_status = HEADSET_OUT;
191 DBG("---- ERROR: on headset headset_in_type error -----\n");
194 if(old_status == headset_info->headset_status)
196 DBG("old_status == headset_info->headset_status\n");
200 switch(pdata->headset_in_type)
202 case HEADSET_IN_HIGH:
205 DBG("--- HEADSET_IN_HIGH headset in HIGH---\n");
206 headset_info->cur_headset_status = BIT_HEADSET_NO_MIC;
207 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);//
208 if (pdata->Hook_gpio) {
209 del_timer(&headset_info->headset_timer);//Start the timer, wait for switch to the headphone channel
210 // headset_info->headset_timer.expires = jiffies + 500;
211 headset_info->headset_timer.expires = jiffies + 10;
212 add_timer(&headset_info->headset_timer);
217 DBG("---HEADSET_IN_HIGH headset out HIGH---\n");
218 if(headset_info->isHook_irq == enable)
220 DBG("disable headset_hook irq\n");
221 headset_info->isHook_irq = disable;
222 disable_irq(headset_info->irq[HOOK]);
224 headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
225 #ifdef CONFIG_SND_RK_SOC_RK2928
226 rk2928_codec_set_spk(HEADSET_OUT);
228 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);//
229 rk28_send_wakeup_key();
230 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
231 DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);
237 DBG("---HEADSET_IN_LOW headset in LOW ---\n");
238 headset_info->cur_headset_status = BIT_HEADSET_NO_MIC;
239 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);//
240 if (pdata->Hook_gpio) {
241 del_timer(&headset_info->headset_timer);//Start the timer, wait for switch to the headphone channel
242 // headset_info->headset_timer.expires = jiffies + 500;
243 headset_info->headset_timer.expires = jiffies + 10;
244 add_timer(&headset_info->headset_timer);
249 DBG("---HEADSET_IN_LOW headset out LOW ---\n");
250 if(headset_info->isHook_irq == enable)
252 DBG("disable headset_hook irq\n");
253 headset_info->isHook_irq = disable;
254 disable_irq(headset_info->irq[HOOK]);
256 #ifdef CONFIG_SND_RK_SOC_RK2928
257 rk2928_codec_set_spk(HEADSET_OUT);
259 headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
260 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);//
261 rk28_send_wakeup_key();
262 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
263 DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);
267 DBG("---- ERROR: on headset headset_in_type error -----\n");
273 mutex_unlock(&headset_info->mutex_lock[HEADSET]);
276 static void Hook_work(struct work_struct *work)
279 struct rk_headset_pdata *pdata = headset_info->pdata;
280 static unsigned int old_status = HOOK_UP;
282 DBG("---Hook_work---\n");
283 mutex_lock(&headset_info->mutex_lock[HOOK]);
284 if(headset_info->headset_status == HEADSET_OUT)
286 DBG("Headset is out\n");
289 #ifdef CONFIG_SND_SOC_WM8994
290 if(wm8994_set_status() != 0)
292 DBG("wm8994 is not set on heatset channel or suspend\n");
298 level = gpio_get_value(pdata->Hook_gpio);
301 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Hook_gpio,i);
310 printk("%s:get pin level err!\n",__FUNCTION__);
314 old_status = headset_info->hook_status;
316 headset_info->hook_status = HOOK_UP;
318 headset_info->hook_status = HOOK_DOWN;
319 if(old_status == headset_info->hook_status)
321 DBG("old_status == headset_info->hook_status\n");
327 DBG("---HOOK Down ---\n");
328 headset_change_irqtype(HOOK,IRQF_TRIGGER_RISING);//
329 input_report_key(headset_info->input_dev,pdata->hook_key_code,headset_info->hook_status);
330 input_sync(headset_info->input_dev);
334 DBG("---HOOK Up ---\n");
335 headset_change_irqtype(HOOK,IRQF_TRIGGER_FALLING);//
336 input_report_key(headset_info->input_dev,pdata->hook_key_code,headset_info->hook_status);
337 input_sync(headset_info->input_dev);
340 mutex_unlock(&headset_info->mutex_lock[HOOK]);
343 static void headset_timer_callback(unsigned long arg)
345 struct headset_priv *headset = (struct headset_priv *)(arg);
346 struct rk_headset_pdata *pdata = headset->pdata;
349 // printk("headset_timer_callback,headset->headset_status=%d\n",headset->headset_status);
351 if(headset->headset_status == HEADSET_OUT)
353 printk("Headset is out\n");
356 #ifdef CONFIG_SND_SOC_WM8994
357 if(wm8994_set_status() != 0)
359 // printk("wait wm8994 set the MICB2\n");
360 // headset_info->headset_timer.expires = jiffies + 500;
361 headset_info->headset_timer.expires = jiffies + 10;
362 add_timer(&headset_info->headset_timer);
368 level = gpio_get_value(pdata->Hook_gpio);
371 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Hook_gpio,i);
380 printk("%s:get pin level err!\n",__FUNCTION__);
386 headset->isMic= 0;//No microphone
387 headset_info->cur_headset_status = BIT_HEADSET_NO_MIC;
388 printk("headset->isMic = %d\n",headset->isMic);
392 headset->isMic = 1;//have mic
393 DBG("enable headset_hook irq\n");
394 enable_irq(headset_info->irq[HOOK]);
395 headset->isHook_irq = enable;
396 headset_info->cur_headset_status = BIT_HEADSET;
397 printk("headset->isMic = %d\n",headset->isMic);
399 #ifdef CONFIG_SND_RK_SOC_RK2928
400 rk2928_codec_set_spk(HEADSET_IN);
402 rk28_send_wakeup_key();
403 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
404 DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);
410 static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
412 return sprintf(buf, "Headset\n");
415 #ifdef CONFIG_HAS_EARLYSUSPEND
416 static void headset_early_resume(struct early_suspend *h)
418 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(10));
419 //DBG(">>>>>headset_early_resume\n");
422 static struct early_suspend hs_early_suspend;
425 static int rk_Hskey_open(struct input_dev *dev)
427 //struct rk28_adckey *adckey = input_get_drvdata(dev);
428 // DBG("===========rk_Hskey_open===========\n");
432 static void rk_Hskey_close(struct input_dev *dev)
434 // DBG("===========rk_Hskey_close===========\n");
435 // struct rk28_adckey *adckey = input_get_drvdata(dev);
439 static int rockchip_headsetobserve_probe(struct platform_device *pdev)
442 struct headset_priv *headset;
443 struct rk_headset_pdata *pdata;
445 headset = kzalloc(sizeof(struct headset_priv), GFP_KERNEL);
446 if (headset == NULL) {
447 dev_err(&pdev->dev, "failed to allocate driver data\n");
450 headset->pdata = pdev->dev.platform_data;
451 pdata = headset->pdata;
452 headset->headset_status = HEADSET_OUT;
453 headset->hook_status = HOOK_UP;
454 headset->isHook_irq = disable;
455 headset->cur_headset_status = 0;
456 headset->sdev.name = "h2w";
457 headset->sdev.print_name = h2w_print_name;
458 ret = switch_dev_register(&headset->sdev);
462 mutex_init(&headset->mutex_lock[HEADSET]);
463 mutex_init(&headset->mutex_lock[HOOK]);
465 INIT_DELAYED_WORK(&headset->h_delayed_work[HEADSET], headsetobserve_work);
466 INIT_DELAYED_WORK(&headset->h_delayed_work[HOOK], Hook_work);
468 // init_timer(&headset->headset_timer);
469 // headset->headset_timer.function = headset_timer_callback;
470 // headset->headset_timer.data = (unsigned long)headset;
471 // headset->headset_timer.expires = jiffies + 3000;
473 setup_timer(&headset->headset_timer, headset_timer_callback, (unsigned long)headset);
474 // headset->headset_timer.expires = jiffies + 1000;
475 // add_timer(&headset->headset_timer);
477 // Create and register the input driver.
478 headset->input_dev = input_allocate_device();
479 if (!headset->input_dev) {
480 dev_err(&pdev->dev, "failed to allocate input device\n");
484 headset->input_dev->name = pdev->name;
485 headset->input_dev->open = rk_Hskey_open;
486 headset->input_dev->close = rk_Hskey_close;
487 headset->input_dev->dev.parent = &pdev->dev;
488 //input_dev->phys = KEY_PHYS_NAME;
489 headset->input_dev->id.vendor = 0x0001;
490 headset->input_dev->id.product = 0x0001;
491 headset->input_dev->id.version = 0x0100;
492 // Register the input device
493 ret = input_register_device(headset->input_dev);
495 dev_err(&pdev->dev, "failed to register input device\n");
496 goto failed_free_dev;
500 // headset->input_dev->keycode = headset->keycodes;
501 // headset->input_dev->keycodesize = sizeof(unsigned char);
502 // headset->input_dev->keycodemax = 2;
504 // set_bit(KEY_MEDIA, headset->input_dev->keybit);
505 // clear_bit(0, headset->input_dev->keybit);
506 input_set_capability(headset->input_dev, EV_KEY, pdata->hook_key_code);
507 // input_set_capability(headset->input_dev, EV_SW, SW_HEADPHONE_INSERT);
508 // input_set_capability(headset->input_dev, EV_KEY, KEY_END);
510 // headset->input_dev->evbit[0] = BIT_MASK(EV_KEY);
512 headset_info = headset;
513 schedule_delayed_work(&headset->h_delayed_work[HEADSET], msecs_to_jiffies(500));
515 #ifdef CONFIG_HAS_EARLYSUSPEND
516 hs_early_suspend.suspend = NULL;
517 hs_early_suspend.resume = headset_early_resume;
518 hs_early_suspend.level = ~0x0;
519 register_early_suspend(&hs_early_suspend);
522 //------------------------------------------------------------------
523 if (pdata->Headset_gpio) {
524 ret = pdata->headset_io_init(pdata->Headset_gpio, pdata->headset_gpio_info.iomux_name, pdata->headset_gpio_info.iomux_mode);
528 headset->irq[HEADSET] = gpio_to_irq(pdata->Headset_gpio);
530 if(pdata->headset_in_type == HEADSET_IN_HIGH)
531 headset->irq_type[HEADSET] = IRQF_TRIGGER_RISING;
533 headset->irq_type[HEADSET] = IRQF_TRIGGER_FALLING;
534 ret = request_irq(headset->irq[HEADSET], headset_interrupt, headset->irq_type[HEADSET], "headset_input", NULL);
536 goto failed_free_dev;
537 enable_irq_wake(headset->irq[HEADSET]);
540 goto failed_free_dev;
541 //------------------------------------------------------------------
542 if (pdata->Hook_gpio) {
543 ret = pdata->hook_io_init(pdata->Hook_gpio, pdata->hook_gpio_info.iomux_name, pdata->hook_gpio_info.iomux_mode);
546 headset->irq[HOOK] = gpio_to_irq(pdata->Hook_gpio);
547 headset->irq_type[HOOK] = IRQF_TRIGGER_FALLING;
549 ret = request_irq(headset->irq[HOOK], Hook_interrupt, headset->irq_type[HOOK] , "headset_hook", NULL);
551 goto failed_free_dev;
552 disable_irq(headset->irq[HOOK]);
554 //------------------------------------------------------------------
559 platform_set_drvdata(pdev, NULL);
560 input_free_device(headset->input_dev);
562 dev_err(&pdev->dev, "failed to headset probe\n");
567 static int rockchip_headsetobserve_suspend(struct platform_device *pdev, pm_message_t state)
569 DBG("%s----%d\n",__FUNCTION__,__LINE__);
570 disable_irq(headset_info->irq[HEADSET]);
571 disable_irq(headset_info->irq[HOOK]);
576 static int rockchip_headsetobserve_resume(struct platform_device *pdev)
578 DBG("%s----%d\n",__FUNCTION__,__LINE__);
579 enable_irq(headset_info->irq[HEADSET]);
580 enable_irq(headset_info->irq[HOOK]);
585 static struct platform_driver rockchip_headsetobserve_driver = {
586 .probe = rockchip_headsetobserve_probe,
587 // .resume = rockchip_headsetobserve_resume,
588 // .suspend = rockchip_headsetobserve_suspend,
590 .name = "rk_headsetdet",
591 .owner = THIS_MODULE,
595 static int __init rockchip_headsetobserve_init(void)
597 platform_driver_register(&rockchip_headsetobserve_driver);
600 module_init(rockchip_headsetobserve_init);
601 MODULE_DESCRIPTION("Rockchip Headset Driver");
602 MODULE_LICENSE("GPL");