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)
65 #ifdef CONFIG_SND_SOC_WM8994
66 extern int wm8994_set_status(void);
69 /* headset private data */
71 struct input_dev *input_dev;
72 struct rk_headset_pdata *pdata;
73 unsigned int headset_status:1;
74 unsigned int hook_status:1;
76 unsigned int isHook_irq:1;
77 int cur_headset_status;
80 unsigned int irq_type[2];
81 struct delayed_work h_delayed_work[2];
82 struct switch_dev sdev;
83 struct mutex mutex_lock[2];
84 struct timer_list headset_timer;
85 unsigned char *keycodes;
87 static struct headset_priv *headset_info;
89 int Headset_isMic(void)
91 return headset_info->isMic;
93 EXPORT_SYMBOL_GPL(Headset_isMic);
95 static irqreturn_t headset_interrupt(int irq, void *dev_id)
97 DBG("---headset_interrupt---\n");
98 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(50));
102 static irqreturn_t Hook_interrupt(int irq, void *dev_id)
104 DBG("---Hook_interrupt---\n");
105 // disable_irq_nosync(headset_info->irq[HOOK]);
106 schedule_delayed_work(&headset_info->h_delayed_work[HOOK], msecs_to_jiffies(100));
110 static int headset_change_irqtype(int type,unsigned int irq_type)
113 DBG("--------%s----------\n",__FUNCTION__);
114 free_irq(headset_info->irq[type],NULL);
119 ret = request_irq(headset_info->irq[type], Hook_interrupt, irq_type, "headset_input", NULL);
122 ret = request_irq(headset_info->irq[type], headset_interrupt, irq_type, "headset_hook", NULL);
131 DBG("headset_change_irqtype: request irq failed\n");
137 static void headsetobserve_work(struct work_struct *work)
140 struct rk_headset_pdata *pdata = headset_info->pdata;
141 static unsigned int old_status = 0;
142 DBG("---headsetobserve_work---\n");
143 mutex_lock(&headset_info->mutex_lock[HEADSET]);
147 level = gpio_get_value(pdata->Headset_gpio);
150 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Headset_gpio,i);
159 printk("%s:get pin level err!\n",__FUNCTION__);
163 old_status = headset_info->headset_status;
164 switch(pdata->headset_in_type)
166 case HEADSET_IN_HIGH:
168 headset_info->headset_status = HEADSET_IN;
170 headset_info->headset_status = HEADSET_OUT;
174 headset_info->headset_status = HEADSET_IN;
176 headset_info->headset_status = HEADSET_OUT;
179 DBG("---- ERROR: on headset headset_in_type error -----\n");
182 if(old_status == headset_info->headset_status)
184 DBG("old_status == headset_info->headset_status\n");
188 switch(pdata->headset_in_type)
190 case HEADSET_IN_HIGH:
193 DBG("--- HEADSET_IN_HIGH headset in HIGH---\n");
194 headset_info->cur_headset_status = BIT_HEADSET_NO_MIC;
195 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);//
196 if (pdata->Hook_gpio) {
197 del_timer(&headset_info->headset_timer);//Start the timer, wait for switch to the headphone channel
198 // headset_info->headset_timer.expires = jiffies + 500;
199 headset_info->headset_timer.expires = jiffies + 10;
200 add_timer(&headset_info->headset_timer);
205 DBG("---HEADSET_IN_HIGH headset out HIGH---\n");
206 if(headset_info->isHook_irq == enable)
208 DBG("disable headset_hook irq\n");
209 headset_info->isHook_irq = disable;
210 disable_irq(headset_info->irq[HOOK]);
212 headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
213 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);//
214 rk28_send_wakeup_key();
215 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
216 DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);
222 DBG("---HEADSET_IN_LOW headset in LOW ---\n");
223 headset_info->cur_headset_status = BIT_HEADSET_NO_MIC;
224 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);//
225 if (pdata->Hook_gpio) {
226 del_timer(&headset_info->headset_timer);//Start the timer, wait for switch to the headphone channel
227 // headset_info->headset_timer.expires = jiffies + 500;
228 headset_info->headset_timer.expires = jiffies + 10;
229 add_timer(&headset_info->headset_timer);
234 DBG("---HEADSET_IN_LOW headset out LOW ---\n");
235 if(headset_info->isHook_irq == enable)
237 DBG("disable headset_hook irq\n");
238 headset_info->isHook_irq = disable;
239 disable_irq(headset_info->irq[HOOK]);
241 headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
242 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);//
243 rk28_send_wakeup_key();
244 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
245 DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);
249 DBG("---- ERROR: on headset headset_in_type error -----\n");
255 mutex_unlock(&headset_info->mutex_lock[HEADSET]);
258 static void Hook_work(struct work_struct *work)
261 struct rk_headset_pdata *pdata = headset_info->pdata;
262 static unsigned int old_status = HOOK_UP;
264 DBG("---Hook_work---\n");
265 mutex_lock(&headset_info->mutex_lock[HOOK]);
266 if(headset_info->headset_status == HEADSET_OUT)
268 DBG("Headset is out\n");
271 #ifdef CONFIG_SND_SOC_WM8994
272 if(wm8994_set_status() != 0)
274 DBG("wm8994 is not set on heatset channel or suspend\n");
280 level = gpio_get_value(pdata->Hook_gpio);
283 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Hook_gpio,i);
292 printk("%s:get pin level err!\n",__FUNCTION__);
296 old_status = headset_info->hook_status;
298 headset_info->hook_status = HOOK_UP;
300 headset_info->hook_status = HOOK_DOWN;
301 if(old_status == headset_info->hook_status)
303 DBG("old_status == headset_info->hook_status\n");
309 DBG("---HOOK Down ---\n");
310 headset_change_irqtype(HOOK,IRQF_TRIGGER_RISING);//
311 input_report_key(headset_info->input_dev,pdata->hook_key_code,headset_info->hook_status);
312 input_sync(headset_info->input_dev);
316 DBG("---HOOK Up ---\n");
317 headset_change_irqtype(HOOK,IRQF_TRIGGER_FALLING);//
318 input_report_key(headset_info->input_dev,pdata->hook_key_code,headset_info->hook_status);
319 input_sync(headset_info->input_dev);
322 mutex_unlock(&headset_info->mutex_lock[HOOK]);
325 static void headset_timer_callback(unsigned long arg)
327 struct headset_priv *headset = (struct headset_priv *)(arg);
328 struct rk_headset_pdata *pdata = headset->pdata;
331 // printk("headset_timer_callback,headset->headset_status=%d\n",headset->headset_status);
333 if(headset->headset_status == HEADSET_OUT)
335 printk("Headset is out\n");
338 #ifdef CONFIG_SND_SOC_WM8994
339 if(wm8994_set_status() != 0)
341 // printk("wait wm8994 set the MICB2\n");
342 // headset_info->headset_timer.expires = jiffies + 500;
343 headset_info->headset_timer.expires = jiffies + 10;
344 add_timer(&headset_info->headset_timer);
350 level = gpio_get_value(pdata->Hook_gpio);
353 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Hook_gpio,i);
362 printk("%s:get pin level err!\n",__FUNCTION__);
368 headset->isMic= 0;//No microphone
369 headset_info->cur_headset_status = BIT_HEADSET_NO_MIC;
370 printk("headset->isMic = %d\n",headset->isMic);
374 headset->isMic = 1;//have mic
375 DBG("enable headset_hook irq\n");
376 enable_irq(headset_info->irq[HOOK]);
377 headset->isHook_irq = enable;
378 headset_info->cur_headset_status = BIT_HEADSET;
379 printk("headset->isMic = %d\n",headset->isMic);
382 rk28_send_wakeup_key();
383 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
384 DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);
390 static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
392 return sprintf(buf, "Headset\n");
395 #ifdef CONFIG_HAS_EARLYSUSPEND
396 static void headset_early_resume(struct early_suspend *h)
398 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(10));
399 //DBG(">>>>>headset_early_resume\n");
402 static struct early_suspend hs_early_suspend;
405 static int rk_Hskey_open(struct input_dev *dev)
407 //struct rk28_adckey *adckey = input_get_drvdata(dev);
408 // DBG("===========rk_Hskey_open===========\n");
412 static void rk_Hskey_close(struct input_dev *dev)
414 // DBG("===========rk_Hskey_close===========\n");
415 // struct rk28_adckey *adckey = input_get_drvdata(dev);
419 static int rockchip_headsetobserve_probe(struct platform_device *pdev)
422 struct headset_priv *headset;
423 struct rk_headset_pdata *pdata;
425 headset = kzalloc(sizeof(struct headset_priv), GFP_KERNEL);
426 if (headset == NULL) {
427 dev_err(&pdev->dev, "failed to allocate driver data\n");
430 headset->pdata = pdev->dev.platform_data;
431 pdata = headset->pdata;
432 headset->headset_status = HEADSET_OUT;
433 headset->hook_status = HOOK_UP;
434 headset->isHook_irq = disable;
435 headset->cur_headset_status = 0;
436 headset->sdev.name = "h2w";
437 headset->sdev.print_name = h2w_print_name;
438 ret = switch_dev_register(&headset->sdev);
442 mutex_init(&headset->mutex_lock[HEADSET]);
443 mutex_init(&headset->mutex_lock[HOOK]);
445 INIT_DELAYED_WORK(&headset->h_delayed_work[HEADSET], headsetobserve_work);
446 INIT_DELAYED_WORK(&headset->h_delayed_work[HOOK], Hook_work);
448 // init_timer(&headset->headset_timer);
449 // headset->headset_timer.function = headset_timer_callback;
450 // headset->headset_timer.data = (unsigned long)headset;
451 // headset->headset_timer.expires = jiffies + 3000;
453 setup_timer(&headset->headset_timer, headset_timer_callback, (unsigned long)headset);
454 // headset->headset_timer.expires = jiffies + 1000;
455 // add_timer(&headset->headset_timer);
457 // Create and register the input driver.
458 headset->input_dev = input_allocate_device();
459 if (!headset->input_dev) {
460 dev_err(&pdev->dev, "failed to allocate input device\n");
464 headset->input_dev->name = pdev->name;
465 headset->input_dev->open = rk_Hskey_open;
466 headset->input_dev->close = rk_Hskey_close;
467 headset->input_dev->dev.parent = &pdev->dev;
468 //input_dev->phys = KEY_PHYS_NAME;
469 headset->input_dev->id.vendor = 0x0001;
470 headset->input_dev->id.product = 0x0001;
471 headset->input_dev->id.version = 0x0100;
472 // Register the input device
473 ret = input_register_device(headset->input_dev);
475 dev_err(&pdev->dev, "failed to register input device\n");
476 goto failed_free_dev;
480 // headset->input_dev->keycode = headset->keycodes;
481 // headset->input_dev->keycodesize = sizeof(unsigned char);
482 // headset->input_dev->keycodemax = 2;
484 // set_bit(KEY_MEDIA, headset->input_dev->keybit);
485 // clear_bit(0, headset->input_dev->keybit);
486 input_set_capability(headset->input_dev, EV_KEY, pdata->hook_key_code);
487 // input_set_capability(headset->input_dev, EV_SW, SW_HEADPHONE_INSERT);
488 // input_set_capability(headset->input_dev, EV_KEY, KEY_END);
490 // headset->input_dev->evbit[0] = BIT_MASK(EV_KEY);
492 headset_info = headset;
493 schedule_delayed_work(&headset->h_delayed_work[HEADSET], msecs_to_jiffies(500));
495 #ifdef CONFIG_HAS_EARLYSUSPEND
496 hs_early_suspend.suspend = NULL;
497 hs_early_suspend.resume = headset_early_resume;
498 hs_early_suspend.level = ~0x0;
499 register_early_suspend(&hs_early_suspend);
502 //------------------------------------------------------------------
503 if (pdata->Headset_gpio) {
504 ret = gpio_request(pdata->Headset_gpio, NULL);
506 goto failed_free_dev;
507 gpio_pull_updown(pdata->Headset_gpio, PullDisable);
508 gpio_direction_input(pdata->Headset_gpio);
509 headset->irq[HEADSET] = gpio_to_irq(pdata->Headset_gpio);
511 if(pdata->headset_in_type == HEADSET_IN_HIGH)
512 headset->irq_type[HEADSET] = IRQF_TRIGGER_RISING;
514 headset->irq_type[HEADSET] = IRQF_TRIGGER_FALLING;
515 ret = request_irq(headset->irq[HEADSET], headset_interrupt, headset->irq_type[HEADSET], "headset_input", NULL);
517 goto failed_free_dev;
518 enable_irq_wake(headset->irq[HEADSET]);
521 goto failed_free_dev;
522 //------------------------------------------------------------------
523 if (pdata->Hook_gpio) {
524 ret = gpio_request(pdata->Hook_gpio , NULL);
526 goto failed_free_dev;
527 gpio_pull_updown(pdata->Hook_gpio, PullDisable);
528 gpio_direction_input(pdata->Hook_gpio);
529 headset->irq[HOOK] = gpio_to_irq(pdata->Hook_gpio);
530 headset->irq_type[HOOK] = IRQF_TRIGGER_FALLING;
532 ret = request_irq(headset->irq[HOOK], Hook_interrupt, headset->irq_type[HOOK] , "headset_hook", NULL);
534 goto failed_free_dev;
535 disable_irq(headset->irq[HOOK]);
537 //------------------------------------------------------------------
542 platform_set_drvdata(pdev, NULL);
543 input_free_device(headset->input_dev);
545 dev_err(&pdev->dev, "failed to headset probe\n");
550 static int rockchip_headsetobserve_suspend(struct platform_device *pdev, pm_message_t state)
552 DBG("%s----%d\n",__FUNCTION__,__LINE__);
553 disable_irq(headset_info->irq[HEADSET]);
554 disable_irq(headset_info->irq[HOOK]);
559 static int rockchip_headsetobserve_resume(struct platform_device *pdev)
561 DBG("%s----%d\n",__FUNCTION__,__LINE__);
562 enable_irq(headset_info->irq[HEADSET]);
563 enable_irq(headset_info->irq[HOOK]);
568 static struct platform_driver rockchip_headsetobserve_driver = {
569 .probe = rockchip_headsetobserve_probe,
570 // .resume = rockchip_headsetobserve_resume,
571 // .suspend = rockchip_headsetobserve_suspend,
573 .name = "rk_headsetdet",
574 .owner = THIS_MODULE,
578 static int __init rockchip_headsetobserve_init(void)
580 platform_driver_register(&rockchip_headsetobserve_driver);
583 module_init(rockchip_headsetobserve_init);
584 MODULE_DESCRIPTION("Rockchip Headset Driver");
585 MODULE_LICENSE("GPL");