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>
45 #define DBG(x...) printk(x)
47 #define DBG(x...) do { } while (0)
50 #define BIT_HEADSET (1 << 0)
51 #define BIT_HEADSET_NO_MIC (1 << 1)
63 extern int wm8994_set_status(void);
65 /* headset private data */
67 struct input_dev *input_dev;
68 struct rk_headset_pdata *pdata;
69 unsigned int headset_status:1;
70 unsigned int hook_status:1;
72 unsigned int isHook_irq:1;
73 int cur_headset_status;
76 unsigned int irq_type[2];
77 struct delayed_work h_delayed_work[2];
78 struct switch_dev sdev;
79 struct mutex mutex_lock[2];
80 struct timer_list headset_timer;
81 unsigned char *keycodes;
83 static struct headset_priv *headset_info;
86 static irqreturn_t headset_interrupt(int irq, void *dev_id)
88 // DBG("---headset_interrupt---\n");
89 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(50));
93 static irqreturn_t Hook_interrupt(int irq, void *dev_id)
95 // DBG("---Hook_interrupt---\n");
96 // disable_irq_nosync(headset_info->irq[HOOK]);
97 schedule_delayed_work(&headset_info->h_delayed_work[HOOK], msecs_to_jiffies(100));
101 static int headset_change_irqtype(int type,unsigned int irq_type)
104 // DBG("--------%s----------\n",__FUNCTION__);
105 free_irq(headset_info->irq[type],NULL);
110 ret = request_irq(headset_info->irq[type], Hook_interrupt, irq_type, NULL, NULL);
113 ret = request_irq(headset_info->irq[type], headset_interrupt, irq_type, NULL, NULL);
122 DBG("headset_change_irqtype: request irq failed\n");
128 static void headsetobserve_work(struct work_struct *work)
131 struct rk_headset_pdata *pdata = headset_info->pdata;
132 static unsigned int old_status = 0;
133 // DBG("---headsetobserve_work---\n");
134 mutex_lock(&headset_info->mutex_lock[HEADSET]);
138 level = gpio_get_value(pdata->Headset_gpio);
141 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Headset_gpio,i);
150 printk("%s:get pin level err!\n",__FUNCTION__);
154 old_status = headset_info->headset_status;
155 switch(pdata->headset_in_type)
157 case HEADSET_IN_HIGH:
159 headset_info->headset_status = HEADSET_IN;
161 headset_info->headset_status = HEADSET_OUT;
165 headset_info->headset_status = HEADSET_IN;
167 headset_info->headset_status = HEADSET_OUT;
170 DBG("---- ERROR: on headset headset_in_type error -----\n");
173 if(old_status == headset_info->headset_status)
175 DBG("old_status == headset_info->headset_status\n");
179 switch(pdata->headset_in_type)
181 case HEADSET_IN_HIGH:
184 // DBG("--- HEADSET_IN_HIGH headset in HIGH---\n");
185 // enable_irq(headset_info->irq[HOOK]);
186 headset_info->cur_headset_status = BIT_HEADSET;
187 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);//
188 del_timer(&headset_info->headset_timer);//Start the timer, wait for switch to the headphone channel
189 headset_info->headset_timer.expires = jiffies + 500;
190 add_timer(&headset_info->headset_timer);
194 // DBG("---HEADSET_IN_HIGH headset out HIGH---\n");
195 if(headset_info->isHook_irq == enable)
197 // DBG("disable_irq\n");
198 headset_info->isHook_irq = disable;
199 disable_irq(headset_info->irq[HOOK]);
201 headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
202 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);//
208 // DBG("---HEADSET_IN_LOW headset in LOW ---\n");
209 headset_info->cur_headset_status = BIT_HEADSET;
210 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);//
211 enable_irq(headset_info->irq[HOOK]);
212 del_timer(&headset_info->headset_timer);//Start the timer, wait for switch to the headphone channel
213 headset_info->headset_timer.expires = jiffies + 500;
214 add_timer(&headset_info->headset_timer);
218 // DBG("---HEADSET_IN_LOW headset out LOW ---\n");
219 if(headset_info->isHook_irq == enable)
221 // DBG("disable_irq\n");
222 headset_info->isHook_irq = disable;
223 disable_irq(headset_info->irq[HOOK]);
225 headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
226 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);//
227 disable_irq(headset_info->irq[HOOK]);
231 DBG("---- ERROR: on headset headset_in_type error -----\n");
234 rk28_send_wakeup_key();
235 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
236 DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);
238 mutex_unlock(&headset_info->mutex_lock[HEADSET]);
241 static void Hook_work(struct work_struct *work)
244 struct rk_headset_pdata *pdata = headset_info->pdata;
245 static unsigned int old_status = HOOK_UP;
247 // DBG("---Hook_work---\n");
248 mutex_lock(&headset_info->mutex_lock[HOOK]);
249 if(headset_info->headset_status == HEADSET_OUT)
251 DBG("Headset is out\n");
254 #ifdef CONFIG_SND_SOC_WM8994
255 if(wm8994_set_status() < 0)
257 DBG("wm8994 is not set on heatset channel or suspend\n");
263 level = gpio_get_value(pdata->Hook_gpio);
266 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Hook_gpio,i);
275 printk("%s:get pin level err!\n",__FUNCTION__);
279 old_status = headset_info->hook_status;
281 headset_info->hook_status = HOOK_UP;
283 headset_info->hook_status = HOOK_DOWN;
284 if(old_status == headset_info->hook_status)
286 DBG("old_status == headset_info->hook_status\n");
292 DBG("---HOOK Down ---\n");
293 headset_change_irqtype(HOOK,IRQF_TRIGGER_RISING);//
294 input_report_key(headset_info->input_dev,pdata->hook_key_code,headset_info->hook_status);
295 input_sync(headset_info->input_dev);
299 DBG("---HOOK Up ---\n");
300 headset_change_irqtype(HOOK,IRQF_TRIGGER_FALLING);//
301 input_report_key(headset_info->input_dev,pdata->hook_key_code,headset_info->hook_status);
302 input_sync(headset_info->input_dev);
305 mutex_unlock(&headset_info->mutex_lock[HOOK]);
308 static void headset_timer_callback(unsigned long arg)
310 struct headset_priv *headset = (struct headset_priv *)(arg);
311 struct rk_headset_pdata *pdata = headset->pdata;
314 // DBG("headset_timer_callback\n");
316 if(headset->headset_status == HEADSET_OUT)
318 DBG("Headset is out\n");
321 #ifdef CONFIG_SND_SOC_WM8994
322 if(wm8994_set_status() < 0)
324 DBG("wm8994 is not set on heatset channel\n");
325 headset_info->headset_timer.expires = jiffies + 500;
326 add_timer(&headset_info->headset_timer);
332 level = gpio_get_value(pdata->Hook_gpio);
335 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Hook_gpio,i);
344 printk("%s:get pin level err!\n",__FUNCTION__);
349 headset->isMic= 0;//No microphone
352 headset->isMic = 1;//have mic
353 // DBG("enable_irq\n");
354 enable_irq(headset_info->irq[HOOK]);
355 headset->isHook_irq = enable;
357 DBG("headset->isMic = %d\n",headset->isMic);
362 static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
364 return sprintf(buf, "Headset\n");
367 #ifdef CONFIG_HAS_EARLYSUSPEND
368 static void headset_early_resume(struct early_suspend *h)
370 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(10));
371 //DBG(">>>>>headset_early_resume\n");
374 static struct early_suspend hs_early_suspend;
377 static int rk_Hskey_open(struct input_dev *dev)
379 //struct rk28_adckey *adckey = input_get_drvdata(dev);
380 // DBG("===========rk_Hskey_open===========\n");
384 static void rk_Hskey_close(struct input_dev *dev)
386 // DBG("===========rk_Hskey_close===========\n");
387 // struct rk28_adckey *adckey = input_get_drvdata(dev);
391 static int rockchip_headsetobserve_probe(struct platform_device *pdev)
394 struct headset_priv *headset;
395 struct rk_headset_pdata *pdata;
397 headset = kzalloc(sizeof(struct headset_priv), GFP_KERNEL);
398 if (headset == NULL) {
399 dev_err(&pdev->dev, "failed to allocate driver data\n");
402 headset->pdata = pdev->dev.platform_data;
403 pdata = headset->pdata;
404 headset->headset_status = HEADSET_OUT;
405 headset->hook_status = HOOK_UP;
406 headset->isHook_irq = disable;
407 headset->cur_headset_status = 0;
408 headset->sdev.name = "h2w";
409 headset->sdev.print_name = h2w_print_name;
410 ret = switch_dev_register(&headset->sdev);
414 mutex_init(&headset->mutex_lock[HEADSET]);
415 mutex_init(&headset->mutex_lock[HOOK]);
417 INIT_DELAYED_WORK(&headset->h_delayed_work[HEADSET], headsetobserve_work);
418 INIT_DELAYED_WORK(&headset->h_delayed_work[HOOK], Hook_work);
420 // init_timer(&headset->headset_timer);
421 // headset->headset_timer.function = headset_timer_callback;
422 // headset->headset_timer.data = (unsigned long)headset;
423 // headset->headset_timer.expires = jiffies + 3000;
425 setup_timer(&headset->headset_timer, headset_timer_callback, (unsigned long)headset);
426 // headset->headset_timer.expires = jiffies + 1000;
427 // add_timer(&headset->headset_timer);
428 //------------------------------------------------------------------
429 ret = gpio_request(pdata->Headset_gpio, NULL);
432 gpio_pull_updown(pdata->Headset_gpio, PullDisable);
433 gpio_direction_input(pdata->Headset_gpio);
434 headset->irq[HEADSET] = gpio_to_irq(pdata->Headset_gpio);
436 if(pdata->headset_in_type == HEADSET_IN_HIGH)
437 headset->irq_type[HEADSET] = IRQF_TRIGGER_RISING;
439 headset->irq_type[HEADSET] = IRQF_TRIGGER_FALLING;
440 ret = request_irq(headset->irq[HEADSET], headset_interrupt, headset->irq_type[HEADSET], NULL, NULL);
443 enable_irq_wake(headset->irq[HEADSET]);
444 //------------------------------------------------------------------
445 ret = gpio_request(pdata->Hook_gpio , NULL);
448 gpio_pull_updown(pdata->Hook_gpio, PullDisable);
449 gpio_direction_input(pdata->Hook_gpio);
450 headset->irq[HOOK] = gpio_to_irq(pdata->Hook_gpio);
451 headset->irq_type[HOOK] = IRQF_TRIGGER_FALLING;
453 ret = request_irq(headset->irq[HOOK], Hook_interrupt, headset->irq_type[HOOK] , NULL, NULL);
456 disable_irq(headset->irq[HOOK]);
457 //------------------------------------------------------------------
458 // Create and register the input driver.
459 headset->input_dev = input_allocate_device();
460 if (!headset->input_dev) {
461 dev_err(&pdev->dev, "failed to allocate input device\n");
465 headset->input_dev->name = pdev->name;
466 headset->input_dev->open = rk_Hskey_open;
467 headset->input_dev->close = rk_Hskey_close;
468 headset->input_dev->dev.parent = &pdev->dev;
469 //input_dev->phys = KEY_PHYS_NAME;
470 headset->input_dev->id.vendor = 0x0001;
471 headset->input_dev->id.product = 0x0001;
472 headset->input_dev->id.version = 0x0100;
473 // Register the input device
474 ret = input_register_device(headset->input_dev);
476 dev_err(&pdev->dev, "failed to register input device\n");
477 goto failed_free_dev;
481 // headset->input_dev->keycode = headset->keycodes;
482 // headset->input_dev->keycodesize = sizeof(unsigned char);
483 // headset->input_dev->keycodemax = 2;
485 // set_bit(KEY_MEDIA, headset->input_dev->keybit);
486 // clear_bit(0, headset->input_dev->keybit);
487 input_set_capability(headset->input_dev, EV_KEY, pdata->hook_key_code);
488 // input_set_capability(headset->input_dev, EV_SW, SW_HEADPHONE_INSERT);
489 // input_set_capability(headset->input_dev, EV_KEY, KEY_END);
491 // headset->input_dev->evbit[0] = BIT_MASK(EV_KEY);
493 headset_info = headset;
494 schedule_delayed_work(&headset->h_delayed_work[HEADSET], msecs_to_jiffies(500));
497 #ifdef CONFIG_HAS_EARLYSUSPEND
498 hs_early_suspend.suspend = NULL;
499 hs_early_suspend.resume = headset_early_resume;
500 hs_early_suspend.level = ~0x0;
501 register_early_suspend(&hs_early_suspend);
507 platform_set_drvdata(pdev, NULL);
508 input_free_device(headset->input_dev);
514 static int rockchip_headsetobserve_suspend(struct platform_device *pdev, pm_message_t state)
516 DBG("%s----%d\n",__FUNCTION__,__LINE__);
517 disable_irq(headset_info->irq[HEADSET]);
518 disable_irq(headset_info->irq[HOOK]);
523 static int rockchip_headsetobserve_resume(struct platform_device *pdev)
525 DBG("%s----%d\n",__FUNCTION__,__LINE__);
526 enable_irq(headset_info->irq[HEADSET]);
527 enable_irq(headset_info->irq[HOOK]);
532 static struct platform_driver rockchip_headsetobserve_driver = {
533 .probe = rockchip_headsetobserve_probe,
534 // .resume = rockchip_headsetobserve_resume,
535 // .suspend = rockchip_headsetobserve_suspend,
537 .name = "rk_headsetdet",
538 .owner = THIS_MODULE,
542 static int __init rockchip_headsetobserve_init(void)
544 platform_driver_register(&rockchip_headsetobserve_driver);
547 module_init(rockchip_headsetobserve_init);
548 MODULE_DESCRIPTION("Rockchip Headset Driver");
549 MODULE_LICENSE("GPL");