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, NULL, NULL);
122 ret = request_irq(headset_info->irq[type], headset_interrupt, irq_type, NULL, 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 // enable_irq(headset_info->irq[HOOK]);
195 headset_info->cur_headset_status = BIT_HEADSET;
196 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);//
197 if (pdata->Hook_gpio != NULL) {
198 del_timer(&headset_info->headset_timer);//Start the timer, wait for switch to the headphone channel
199 headset_info->headset_timer.expires = jiffies + 500;
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_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);//
219 // DBG("---HEADSET_IN_LOW headset in LOW ---\n");
220 headset_info->cur_headset_status = BIT_HEADSET;
221 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);//
222 if (pdata->Hook_gpio != NULL) {
223 del_timer(&headset_info->headset_timer);//Start the timer, wait for switch to the headphone channel
224 headset_info->headset_timer.expires = jiffies + 500;
225 add_timer(&headset_info->headset_timer);
230 // DBG("---HEADSET_IN_LOW headset out LOW ---\n");
231 if(headset_info->isHook_irq == enable)
233 // DBG("disable_irq\n");
234 headset_info->isHook_irq = disable;
235 disable_irq(headset_info->irq[HOOK]);
237 headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
238 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);//
242 DBG("---- ERROR: on headset headset_in_type error -----\n");
245 rk28_send_wakeup_key();
246 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
247 DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);
249 mutex_unlock(&headset_info->mutex_lock[HEADSET]);
252 static void Hook_work(struct work_struct *work)
255 struct rk_headset_pdata *pdata = headset_info->pdata;
256 static unsigned int old_status = HOOK_UP;
258 // DBG("---Hook_work---\n");
259 mutex_lock(&headset_info->mutex_lock[HOOK]);
260 if(headset_info->headset_status == HEADSET_OUT)
262 DBG("Headset is out\n");
265 /*#ifdef CONFIG_SND_SOC_WM8994
266 if(wm8994_set_status() < 0)
268 DBG("wm8994 is not set on heatset channel or suspend\n");
274 level = gpio_get_value(pdata->Hook_gpio);
277 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Hook_gpio,i);
286 printk("%s:get pin level err!\n",__FUNCTION__);
290 old_status = headset_info->hook_status;
292 headset_info->hook_status = HOOK_UP;
294 headset_info->hook_status = HOOK_DOWN;
295 if(old_status == headset_info->hook_status)
297 DBG("old_status == headset_info->hook_status\n");
303 DBG("---HOOK Down ---\n");
304 headset_change_irqtype(HOOK,IRQF_TRIGGER_RISING);//
305 input_report_key(headset_info->input_dev,pdata->hook_key_code,headset_info->hook_status);
306 input_sync(headset_info->input_dev);
310 DBG("---HOOK Up ---\n");
311 headset_change_irqtype(HOOK,IRQF_TRIGGER_FALLING);//
312 input_report_key(headset_info->input_dev,pdata->hook_key_code,headset_info->hook_status);
313 input_sync(headset_info->input_dev);
316 mutex_unlock(&headset_info->mutex_lock[HOOK]);
319 static void headset_timer_callback(unsigned long arg)
321 struct headset_priv *headset = (struct headset_priv *)(arg);
322 struct rk_headset_pdata *pdata = headset->pdata;
325 DBG("headset_timer_callback,headset->headset_status=%d\n",headset->headset_status);
327 if(headset->headset_status == HEADSET_OUT)
329 DBG("Headset is out\n");
332 /*#ifdef CONFIG_SND_SOC_WM8994
333 if(wm8994_set_status() < 0)
335 DBG("wm8994 is not set on heatset channel\n");
336 headset_info->headset_timer.expires = jiffies + 500;
337 add_timer(&headset_info->headset_timer);
343 level = gpio_get_value(pdata->Hook_gpio);
346 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Hook_gpio,i);
355 printk("%s:get pin level err!\n",__FUNCTION__);
360 headset->isMic= 0;//No microphone
363 headset->isMic = 1;//have mic
364 // DBG("enable_irq\n");
365 enable_irq(headset_info->irq[HOOK]);
366 headset->isHook_irq = enable;
368 DBG("headset->isMic = %d\n",headset->isMic);
373 static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
375 return sprintf(buf, "Headset\n");
378 #ifdef CONFIG_HAS_EARLYSUSPEND
379 static void headset_early_resume(struct early_suspend *h)
381 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(10));
382 //DBG(">>>>>headset_early_resume\n");
385 static struct early_suspend hs_early_suspend;
388 static int rk_Hskey_open(struct input_dev *dev)
390 //struct rk28_adckey *adckey = input_get_drvdata(dev);
391 // DBG("===========rk_Hskey_open===========\n");
395 static void rk_Hskey_close(struct input_dev *dev)
397 // DBG("===========rk_Hskey_close===========\n");
398 // struct rk28_adckey *adckey = input_get_drvdata(dev);
402 static int rockchip_headsetobserve_probe(struct platform_device *pdev)
405 struct headset_priv *headset;
406 struct rk_headset_pdata *pdata;
408 headset = kzalloc(sizeof(struct headset_priv), GFP_KERNEL);
409 if (headset == NULL) {
410 dev_err(&pdev->dev, "failed to allocate driver data\n");
413 headset->pdata = pdev->dev.platform_data;
414 pdata = headset->pdata;
415 headset->headset_status = HEADSET_OUT;
416 headset->hook_status = HOOK_UP;
417 headset->isHook_irq = disable;
418 headset->cur_headset_status = 0;
419 headset->sdev.name = "h2w";
420 headset->sdev.print_name = h2w_print_name;
421 ret = switch_dev_register(&headset->sdev);
425 mutex_init(&headset->mutex_lock[HEADSET]);
426 mutex_init(&headset->mutex_lock[HOOK]);
428 INIT_DELAYED_WORK(&headset->h_delayed_work[HEADSET], headsetobserve_work);
429 INIT_DELAYED_WORK(&headset->h_delayed_work[HOOK], Hook_work);
431 // init_timer(&headset->headset_timer);
432 // headset->headset_timer.function = headset_timer_callback;
433 // headset->headset_timer.data = (unsigned long)headset;
434 // headset->headset_timer.expires = jiffies + 3000;
436 setup_timer(&headset->headset_timer, headset_timer_callback, (unsigned long)headset);
437 // headset->headset_timer.expires = jiffies + 1000;
438 // add_timer(&headset->headset_timer);
439 //------------------------------------------------------------------
440 if (pdata->Headset_gpio != NULL) {
441 ret = gpio_request(pdata->Headset_gpio, NULL);
444 gpio_pull_updown(pdata->Headset_gpio, PullDisable);
445 gpio_direction_input(pdata->Headset_gpio);
446 headset->irq[HEADSET] = gpio_to_irq(pdata->Headset_gpio);
448 if(pdata->headset_in_type == HEADSET_IN_HIGH)
449 headset->irq_type[HEADSET] = IRQF_TRIGGER_RISING;
451 headset->irq_type[HEADSET] = IRQF_TRIGGER_FALLING;
452 ret = request_irq(headset->irq[HEADSET], headset_interrupt, headset->irq_type[HEADSET], "headset_input", NULL);
455 enable_irq_wake(headset->irq[HEADSET]);
457 //------------------------------------------------------------------
458 if (pdata->Hook_gpio != NULL) {
459 ret = gpio_request(pdata->Hook_gpio , NULL);
462 gpio_pull_updown(pdata->Hook_gpio, PullDisable);
463 gpio_direction_input(pdata->Hook_gpio);
464 headset->irq[HOOK] = gpio_to_irq(pdata->Hook_gpio);
465 headset->irq_type[HOOK] = IRQF_TRIGGER_FALLING;
467 ret = request_irq(headset->irq[HOOK], Hook_interrupt, headset->irq_type[HOOK] , "headset_hook", NULL);
470 disable_irq(headset->irq[HOOK]);
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;
496 // headset->input_dev->keycode = headset->keycodes;
497 // headset->input_dev->keycodesize = sizeof(unsigned char);
498 // headset->input_dev->keycodemax = 2;
500 // set_bit(KEY_MEDIA, headset->input_dev->keybit);
501 // clear_bit(0, headset->input_dev->keybit);
502 input_set_capability(headset->input_dev, EV_KEY, pdata->hook_key_code);
503 // input_set_capability(headset->input_dev, EV_SW, SW_HEADPHONE_INSERT);
504 // input_set_capability(headset->input_dev, EV_KEY, KEY_END);
506 // headset->input_dev->evbit[0] = BIT_MASK(EV_KEY);
508 headset_info = headset;
509 schedule_delayed_work(&headset->h_delayed_work[HEADSET], msecs_to_jiffies(500));
511 #ifdef CONFIG_HAS_EARLYSUSPEND
512 hs_early_suspend.suspend = NULL;
513 hs_early_suspend.resume = headset_early_resume;
514 hs_early_suspend.level = ~0x0;
515 register_early_suspend(&hs_early_suspend);
521 platform_set_drvdata(pdev, NULL);
522 input_free_device(headset->input_dev);
528 static int rockchip_headsetobserve_suspend(struct platform_device *pdev, pm_message_t state)
530 DBG("%s----%d\n",__FUNCTION__,__LINE__);
531 disable_irq(headset_info->irq[HEADSET]);
532 disable_irq(headset_info->irq[HOOK]);
537 static int rockchip_headsetobserve_resume(struct platform_device *pdev)
539 DBG("%s----%d\n",__FUNCTION__,__LINE__);
540 enable_irq(headset_info->irq[HEADSET]);
541 enable_irq(headset_info->irq[HOOK]);
546 static struct platform_driver rockchip_headsetobserve_driver = {
547 .probe = rockchip_headsetobserve_probe,
548 // .resume = rockchip_headsetobserve_resume,
549 // .suspend = rockchip_headsetobserve_suspend,
551 .name = "rk_headsetdet",
552 .owner = THIS_MODULE,
556 static int __init rockchip_headsetobserve_init(void)
558 platform_driver_register(&rockchip_headsetobserve_driver);
561 module_init(rockchip_headsetobserve_init);
562 MODULE_DESCRIPTION("Rockchip Headset Driver");
563 MODULE_LICENSE("GPL");