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;
85 int Headset_isMic(void)
87 return headset_info->isMic;
89 EXPORT_SYMBOL_GPL(Headset_isMic);
91 static irqreturn_t headset_interrupt(int irq, void *dev_id)
93 // DBG("---headset_interrupt---\n");
94 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(50));
98 static irqreturn_t Hook_interrupt(int irq, void *dev_id)
100 // DBG("---Hook_interrupt---\n");
101 // disable_irq_nosync(headset_info->irq[HOOK]);
102 schedule_delayed_work(&headset_info->h_delayed_work[HOOK], msecs_to_jiffies(100));
106 static int headset_change_irqtype(int type,unsigned int irq_type)
109 // DBG("--------%s----------\n",__FUNCTION__);
110 free_irq(headset_info->irq[type],NULL);
115 ret = request_irq(headset_info->irq[type], Hook_interrupt, irq_type, NULL, NULL);
118 ret = request_irq(headset_info->irq[type], headset_interrupt, irq_type, NULL, NULL);
127 DBG("headset_change_irqtype: request irq failed\n");
133 static void headsetobserve_work(struct work_struct *work)
136 struct rk_headset_pdata *pdata = headset_info->pdata;
137 static unsigned int old_status = 0;
138 // DBG("---headsetobserve_work---\n");
139 mutex_lock(&headset_info->mutex_lock[HEADSET]);
143 level = gpio_get_value(pdata->Headset_gpio);
146 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Headset_gpio,i);
155 printk("%s:get pin level err!\n",__FUNCTION__);
159 old_status = headset_info->headset_status;
160 switch(pdata->headset_in_type)
162 case HEADSET_IN_HIGH:
164 headset_info->headset_status = HEADSET_IN;
166 headset_info->headset_status = HEADSET_OUT;
170 headset_info->headset_status = HEADSET_IN;
172 headset_info->headset_status = HEADSET_OUT;
175 DBG("---- ERROR: on headset headset_in_type error -----\n");
178 if(old_status == headset_info->headset_status)
180 DBG("old_status == headset_info->headset_status\n");
184 switch(pdata->headset_in_type)
186 case HEADSET_IN_HIGH:
189 // DBG("--- HEADSET_IN_HIGH headset in HIGH---\n");
190 // enable_irq(headset_info->irq[HOOK]);
191 headset_info->cur_headset_status = BIT_HEADSET;
192 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);//
193 if (pdata->Headset_gpio != NULL) {
194 del_timer(&headset_info->headset_timer);//Start the timer, wait for switch to the headphone channel
195 headset_info->headset_timer.expires = jiffies + 500;
196 add_timer(&headset_info->headset_timer);
201 // DBG("---HEADSET_IN_HIGH headset out HIGH---\n");
202 if(headset_info->isHook_irq == enable)
204 // DBG("disable_irq\n");
205 headset_info->isHook_irq = disable;
206 disable_irq(headset_info->irq[HOOK]);
208 headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
209 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);//
215 // DBG("---HEADSET_IN_LOW headset in LOW ---\n");
216 headset_info->cur_headset_status = BIT_HEADSET;
217 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);//
218 if (pdata->Headset_gpio != NULL) {
219 del_timer(&headset_info->headset_timer);//Start the timer, wait for switch to the headphone channel
220 headset_info->headset_timer.expires = jiffies + 500;
221 add_timer(&headset_info->headset_timer);
226 // DBG("---HEADSET_IN_LOW headset out LOW ---\n");
227 if(headset_info->isHook_irq == enable)
229 // DBG("disable_irq\n");
230 headset_info->isHook_irq = disable;
231 disable_irq(headset_info->irq[HOOK]);
233 headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
234 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);//
238 DBG("---- ERROR: on headset headset_in_type error -----\n");
241 rk28_send_wakeup_key();
242 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
243 DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);
245 mutex_unlock(&headset_info->mutex_lock[HEADSET]);
248 static void Hook_work(struct work_struct *work)
251 struct rk_headset_pdata *pdata = headset_info->pdata;
252 static unsigned int old_status = HOOK_UP;
254 // DBG("---Hook_work---\n");
255 mutex_lock(&headset_info->mutex_lock[HOOK]);
256 if(headset_info->headset_status == HEADSET_OUT)
258 DBG("Headset is out\n");
261 #ifdef CONFIG_SND_SOC_WM8994
262 if(wm8994_set_status() < 0)
264 DBG("wm8994 is not set on heatset channel or suspend\n");
270 level = gpio_get_value(pdata->Hook_gpio);
273 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Hook_gpio,i);
282 printk("%s:get pin level err!\n",__FUNCTION__);
286 old_status = headset_info->hook_status;
288 headset_info->hook_status = HOOK_UP;
290 headset_info->hook_status = HOOK_DOWN;
291 if(old_status == headset_info->hook_status)
293 DBG("old_status == headset_info->hook_status\n");
299 DBG("---HOOK Down ---\n");
300 headset_change_irqtype(HOOK,IRQF_TRIGGER_RISING);//
301 input_report_key(headset_info->input_dev,pdata->hook_key_code,headset_info->hook_status);
302 input_sync(headset_info->input_dev);
306 DBG("---HOOK Up ---\n");
307 headset_change_irqtype(HOOK,IRQF_TRIGGER_FALLING);//
308 input_report_key(headset_info->input_dev,pdata->hook_key_code,headset_info->hook_status);
309 input_sync(headset_info->input_dev);
312 mutex_unlock(&headset_info->mutex_lock[HOOK]);
315 static void headset_timer_callback(unsigned long arg)
317 struct headset_priv *headset = (struct headset_priv *)(arg);
318 struct rk_headset_pdata *pdata = headset->pdata;
321 DBG("headset_timer_callback,headset->headset_status=%d\n",headset->headset_status);
323 if(headset->headset_status == HEADSET_OUT)
325 DBG("Headset is out\n");
328 #ifdef CONFIG_SND_SOC_WM8994
329 if(wm8994_set_status() < 0)
331 DBG("wm8994 is not set on heatset channel\n");
332 headset_info->headset_timer.expires = jiffies + 500;
333 add_timer(&headset_info->headset_timer);
339 level = gpio_get_value(pdata->Hook_gpio);
342 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Hook_gpio,i);
351 printk("%s:get pin level err!\n",__FUNCTION__);
356 headset->isMic= 0;//No microphone
359 headset->isMic = 1;//have mic
360 // DBG("enable_irq\n");
361 enable_irq(headset_info->irq[HOOK]);
362 headset->isHook_irq = enable;
364 DBG("headset->isMic = %d\n",headset->isMic);
369 static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
371 return sprintf(buf, "Headset\n");
374 #ifdef CONFIG_HAS_EARLYSUSPEND
375 static void headset_early_resume(struct early_suspend *h)
377 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(10));
378 //DBG(">>>>>headset_early_resume\n");
381 static struct early_suspend hs_early_suspend;
384 static int rk_Hskey_open(struct input_dev *dev)
386 //struct rk28_adckey *adckey = input_get_drvdata(dev);
387 // DBG("===========rk_Hskey_open===========\n");
391 static void rk_Hskey_close(struct input_dev *dev)
393 // DBG("===========rk_Hskey_close===========\n");
394 // struct rk28_adckey *adckey = input_get_drvdata(dev);
398 static int rockchip_headsetobserve_probe(struct platform_device *pdev)
401 struct headset_priv *headset;
402 struct rk_headset_pdata *pdata;
404 headset = kzalloc(sizeof(struct headset_priv), GFP_KERNEL);
405 if (headset == NULL) {
406 dev_err(&pdev->dev, "failed to allocate driver data\n");
409 headset->pdata = pdev->dev.platform_data;
410 pdata = headset->pdata;
411 headset->headset_status = HEADSET_OUT;
412 headset->hook_status = HOOK_UP;
413 headset->isHook_irq = disable;
414 headset->cur_headset_status = 0;
415 headset->sdev.name = "h2w";
416 headset->sdev.print_name = h2w_print_name;
417 ret = switch_dev_register(&headset->sdev);
421 mutex_init(&headset->mutex_lock[HEADSET]);
422 mutex_init(&headset->mutex_lock[HOOK]);
424 INIT_DELAYED_WORK(&headset->h_delayed_work[HEADSET], headsetobserve_work);
425 INIT_DELAYED_WORK(&headset->h_delayed_work[HOOK], Hook_work);
427 // init_timer(&headset->headset_timer);
428 // headset->headset_timer.function = headset_timer_callback;
429 // headset->headset_timer.data = (unsigned long)headset;
430 // headset->headset_timer.expires = jiffies + 3000;
432 setup_timer(&headset->headset_timer, headset_timer_callback, (unsigned long)headset);
433 // headset->headset_timer.expires = jiffies + 1000;
434 // add_timer(&headset->headset_timer);
435 //------------------------------------------------------------------
436 if (pdata->Headset_gpio != NULL) {
437 ret = gpio_request(pdata->Headset_gpio, NULL);
440 gpio_pull_updown(pdata->Headset_gpio, PullDisable);
441 gpio_direction_input(pdata->Headset_gpio);
442 headset->irq[HEADSET] = gpio_to_irq(pdata->Headset_gpio);
444 if(pdata->headset_in_type == HEADSET_IN_HIGH)
445 headset->irq_type[HEADSET] = IRQF_TRIGGER_RISING;
447 headset->irq_type[HEADSET] = IRQF_TRIGGER_FALLING;
448 ret = request_irq(headset->irq[HEADSET], headset_interrupt, headset->irq_type[HEADSET], "headset_input", NULL);
451 enable_irq_wake(headset->irq[HEADSET]);
453 //------------------------------------------------------------------
454 if (pdata->Hook_gpio != NULL) {
455 ret = gpio_request(pdata->Hook_gpio , NULL);
458 gpio_pull_updown(pdata->Hook_gpio, PullDisable);
459 gpio_direction_input(pdata->Hook_gpio);
460 headset->irq[HOOK] = gpio_to_irq(pdata->Hook_gpio);
461 headset->irq_type[HOOK] = IRQF_TRIGGER_FALLING;
463 ret = request_irq(headset->irq[HOOK], Hook_interrupt, headset->irq_type[HOOK] , "headset_hook", NULL);
466 disable_irq(headset->irq[HOOK]);
468 //------------------------------------------------------------------
469 // Create and register the input driver.
470 headset->input_dev = input_allocate_device();
471 if (!headset->input_dev) {
472 dev_err(&pdev->dev, "failed to allocate input device\n");
476 headset->input_dev->name = pdev->name;
477 headset->input_dev->open = rk_Hskey_open;
478 headset->input_dev->close = rk_Hskey_close;
479 headset->input_dev->dev.parent = &pdev->dev;
480 //input_dev->phys = KEY_PHYS_NAME;
481 headset->input_dev->id.vendor = 0x0001;
482 headset->input_dev->id.product = 0x0001;
483 headset->input_dev->id.version = 0x0100;
484 // Register the input device
485 ret = input_register_device(headset->input_dev);
487 dev_err(&pdev->dev, "failed to register input device\n");
488 goto failed_free_dev;
492 // headset->input_dev->keycode = headset->keycodes;
493 // headset->input_dev->keycodesize = sizeof(unsigned char);
494 // headset->input_dev->keycodemax = 2;
496 // set_bit(KEY_MEDIA, headset->input_dev->keybit);
497 // clear_bit(0, headset->input_dev->keybit);
498 input_set_capability(headset->input_dev, EV_KEY, pdata->hook_key_code);
499 // input_set_capability(headset->input_dev, EV_SW, SW_HEADPHONE_INSERT);
500 // input_set_capability(headset->input_dev, EV_KEY, KEY_END);
502 // headset->input_dev->evbit[0] = BIT_MASK(EV_KEY);
504 headset_info = headset;
505 schedule_delayed_work(&headset->h_delayed_work[HEADSET], msecs_to_jiffies(500));
507 #ifdef CONFIG_HAS_EARLYSUSPEND
508 hs_early_suspend.suspend = NULL;
509 hs_early_suspend.resume = headset_early_resume;
510 hs_early_suspend.level = ~0x0;
511 register_early_suspend(&hs_early_suspend);
517 platform_set_drvdata(pdev, NULL);
518 input_free_device(headset->input_dev);
524 static int rockchip_headsetobserve_suspend(struct platform_device *pdev, pm_message_t state)
526 DBG("%s----%d\n",__FUNCTION__,__LINE__);
527 disable_irq(headset_info->irq[HEADSET]);
528 disable_irq(headset_info->irq[HOOK]);
533 static int rockchip_headsetobserve_resume(struct platform_device *pdev)
535 DBG("%s----%d\n",__FUNCTION__,__LINE__);
536 enable_irq(headset_info->irq[HEADSET]);
537 enable_irq(headset_info->irq[HOOK]);
542 static struct platform_driver rockchip_headsetobserve_driver = {
543 .probe = rockchip_headsetobserve_probe,
544 // .resume = rockchip_headsetobserve_resume,
545 // .suspend = rockchip_headsetobserve_suspend,
547 .name = "rk_headsetdet",
548 .owner = THIS_MODULE,
552 static int __init rockchip_headsetobserve_init(void)
554 platform_driver_register(&rockchip_headsetobserve_driver);
557 module_init(rockchip_headsetobserve_init);
558 MODULE_DESCRIPTION("Rockchip Headset Driver");
559 MODULE_LICENSE("GPL");