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 extern int wm8994_set_status(void);
66 /* headset private data */
68 struct input_dev *input_dev;
69 struct rk_headset_pdata *pdata;
70 unsigned int headset_status:1;
71 unsigned int hook_status:1;
73 unsigned int isHook_irq:1;
74 int cur_headset_status;
77 unsigned int irq_type[2];
78 struct delayed_work h_delayed_work[2];
79 struct switch_dev sdev;
80 struct mutex mutex_lock[2];
81 struct timer_list headset_timer;
82 unsigned char *keycodes;
84 static struct headset_priv *headset_info;
86 int Headset_isMic(void)
88 return headset_info->isMic;
90 EXPORT_SYMBOL_GPL(Headset_isMic);
92 static irqreturn_t headset_interrupt(int irq, void *dev_id)
94 // DBG("---headset_interrupt---\n");
95 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(50));
99 static irqreturn_t Hook_interrupt(int irq, void *dev_id)
101 // DBG("---Hook_interrupt---\n");
102 // disable_irq_nosync(headset_info->irq[HOOK]);
103 schedule_delayed_work(&headset_info->h_delayed_work[HOOK], msecs_to_jiffies(100));
107 static int headset_change_irqtype(int type,unsigned int irq_type)
110 // DBG("--------%s----------\n",__FUNCTION__);
111 free_irq(headset_info->irq[type],NULL);
116 ret = request_irq(headset_info->irq[type], Hook_interrupt, irq_type, NULL, NULL);
119 ret = request_irq(headset_info->irq[type], headset_interrupt, irq_type, NULL, NULL);
128 DBG("headset_change_irqtype: request irq failed\n");
134 static void headsetobserve_work(struct work_struct *work)
137 struct rk_headset_pdata *pdata = headset_info->pdata;
138 static unsigned int old_status = 0;
139 // DBG("---headsetobserve_work---\n");
140 mutex_lock(&headset_info->mutex_lock[HEADSET]);
144 level = gpio_get_value(pdata->Headset_gpio);
147 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Headset_gpio,i);
156 printk("%s:get pin level err!\n",__FUNCTION__);
160 old_status = headset_info->headset_status;
161 switch(pdata->headset_in_type)
163 case HEADSET_IN_HIGH:
165 headset_info->headset_status = HEADSET_IN;
167 headset_info->headset_status = HEADSET_OUT;
171 headset_info->headset_status = HEADSET_IN;
173 headset_info->headset_status = HEADSET_OUT;
176 DBG("---- ERROR: on headset headset_in_type error -----\n");
179 if(old_status == headset_info->headset_status)
181 DBG("old_status == headset_info->headset_status\n");
185 switch(pdata->headset_in_type)
187 case HEADSET_IN_HIGH:
190 // DBG("--- HEADSET_IN_HIGH headset in HIGH---\n");
191 // enable_irq(headset_info->irq[HOOK]);
192 headset_info->cur_headset_status = BIT_HEADSET;
193 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);//
194 if (pdata->Headset_gpio != NULL) {
195 del_timer(&headset_info->headset_timer);//Start the timer, wait for switch to the headphone channel
196 headset_info->headset_timer.expires = jiffies + 500;
197 add_timer(&headset_info->headset_timer);
202 // DBG("---HEADSET_IN_HIGH headset out HIGH---\n");
203 if(headset_info->isHook_irq == enable)
205 // DBG("disable_irq\n");
206 headset_info->isHook_irq = disable;
207 disable_irq(headset_info->irq[HOOK]);
209 headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
210 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);//
216 // DBG("---HEADSET_IN_LOW headset in LOW ---\n");
217 headset_info->cur_headset_status = BIT_HEADSET;
218 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);//
219 if (pdata->Headset_gpio != NULL) {
220 del_timer(&headset_info->headset_timer);//Start the timer, wait for switch to the headphone channel
221 headset_info->headset_timer.expires = jiffies + 500;
222 add_timer(&headset_info->headset_timer);
227 // DBG("---HEADSET_IN_LOW headset out LOW ---\n");
228 if(headset_info->isHook_irq == enable)
230 // DBG("disable_irq\n");
231 headset_info->isHook_irq = disable;
232 disable_irq(headset_info->irq[HOOK]);
234 headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
235 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);//
239 DBG("---- ERROR: on headset headset_in_type error -----\n");
242 rk28_send_wakeup_key();
243 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
244 DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);
246 mutex_unlock(&headset_info->mutex_lock[HEADSET]);
249 static void Hook_work(struct work_struct *work)
252 struct rk_headset_pdata *pdata = headset_info->pdata;
253 static unsigned int old_status = HOOK_UP;
255 // DBG("---Hook_work---\n");
256 mutex_lock(&headset_info->mutex_lock[HOOK]);
257 if(headset_info->headset_status == HEADSET_OUT)
259 DBG("Headset is out\n");
262 #ifdef CONFIG_SND_SOC_WM8994
263 if(wm8994_set_status() < 0)
265 DBG("wm8994 is not set on heatset channel or suspend\n");
271 level = gpio_get_value(pdata->Hook_gpio);
274 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Hook_gpio,i);
283 printk("%s:get pin level err!\n",__FUNCTION__);
287 old_status = headset_info->hook_status;
289 headset_info->hook_status = HOOK_UP;
291 headset_info->hook_status = HOOK_DOWN;
292 if(old_status == headset_info->hook_status)
294 DBG("old_status == headset_info->hook_status\n");
300 DBG("---HOOK Down ---\n");
301 headset_change_irqtype(HOOK,IRQF_TRIGGER_RISING);//
302 input_report_key(headset_info->input_dev,pdata->hook_key_code,headset_info->hook_status);
303 input_sync(headset_info->input_dev);
307 DBG("---HOOK Up ---\n");
308 headset_change_irqtype(HOOK,IRQF_TRIGGER_FALLING);//
309 input_report_key(headset_info->input_dev,pdata->hook_key_code,headset_info->hook_status);
310 input_sync(headset_info->input_dev);
313 mutex_unlock(&headset_info->mutex_lock[HOOK]);
316 static void headset_timer_callback(unsigned long arg)
318 struct headset_priv *headset = (struct headset_priv *)(arg);
319 struct rk_headset_pdata *pdata = headset->pdata;
322 DBG("headset_timer_callback,headset->headset_status=%d\n",headset->headset_status);
324 if(headset->headset_status == HEADSET_OUT)
326 DBG("Headset is out\n");
329 #ifdef CONFIG_SND_SOC_WM8994
330 if(wm8994_set_status() < 0)
332 DBG("wm8994 is not set on heatset channel\n");
333 headset_info->headset_timer.expires = jiffies + 500;
334 add_timer(&headset_info->headset_timer);
340 level = gpio_get_value(pdata->Hook_gpio);
343 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Hook_gpio,i);
352 printk("%s:get pin level err!\n",__FUNCTION__);
357 headset->isMic= 0;//No microphone
360 headset->isMic = 1;//have mic
361 // DBG("enable_irq\n");
362 enable_irq(headset_info->irq[HOOK]);
363 headset->isHook_irq = enable;
365 DBG("headset->isMic = %d\n",headset->isMic);
370 static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
372 return sprintf(buf, "Headset\n");
375 #ifdef CONFIG_HAS_EARLYSUSPEND
376 static void headset_early_resume(struct early_suspend *h)
378 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(10));
379 //DBG(">>>>>headset_early_resume\n");
382 static struct early_suspend hs_early_suspend;
385 static int rk_Hskey_open(struct input_dev *dev)
387 //struct rk28_adckey *adckey = input_get_drvdata(dev);
388 // DBG("===========rk_Hskey_open===========\n");
392 static void rk_Hskey_close(struct input_dev *dev)
394 // DBG("===========rk_Hskey_close===========\n");
395 // struct rk28_adckey *adckey = input_get_drvdata(dev);
399 static int rockchip_headsetobserve_probe(struct platform_device *pdev)
402 struct headset_priv *headset;
403 struct rk_headset_pdata *pdata;
405 headset = kzalloc(sizeof(struct headset_priv), GFP_KERNEL);
406 if (headset == NULL) {
407 dev_err(&pdev->dev, "failed to allocate driver data\n");
410 headset->pdata = pdev->dev.platform_data;
411 pdata = headset->pdata;
412 headset->headset_status = HEADSET_OUT;
413 headset->hook_status = HOOK_UP;
414 headset->isHook_irq = disable;
415 headset->cur_headset_status = 0;
416 headset->sdev.name = "h2w";
417 headset->sdev.print_name = h2w_print_name;
418 ret = switch_dev_register(&headset->sdev);
422 mutex_init(&headset->mutex_lock[HEADSET]);
423 mutex_init(&headset->mutex_lock[HOOK]);
425 INIT_DELAYED_WORK(&headset->h_delayed_work[HEADSET], headsetobserve_work);
426 INIT_DELAYED_WORK(&headset->h_delayed_work[HOOK], Hook_work);
428 // init_timer(&headset->headset_timer);
429 // headset->headset_timer.function = headset_timer_callback;
430 // headset->headset_timer.data = (unsigned long)headset;
431 // headset->headset_timer.expires = jiffies + 3000;
433 setup_timer(&headset->headset_timer, headset_timer_callback, (unsigned long)headset);
434 // headset->headset_timer.expires = jiffies + 1000;
435 // add_timer(&headset->headset_timer);
436 //------------------------------------------------------------------
437 if (pdata->Headset_gpio != NULL) {
438 ret = gpio_request(pdata->Headset_gpio, NULL);
441 gpio_pull_updown(pdata->Headset_gpio, PullDisable);
442 gpio_direction_input(pdata->Headset_gpio);
443 headset->irq[HEADSET] = gpio_to_irq(pdata->Headset_gpio);
445 if(pdata->headset_in_type == HEADSET_IN_HIGH)
446 headset->irq_type[HEADSET] = IRQF_TRIGGER_RISING;
448 headset->irq_type[HEADSET] = IRQF_TRIGGER_FALLING;
449 ret = request_irq(headset->irq[HEADSET], headset_interrupt, headset->irq_type[HEADSET], "headset_input", NULL);
452 enable_irq_wake(headset->irq[HEADSET]);
454 //------------------------------------------------------------------
455 if (pdata->Hook_gpio != NULL) {
456 ret = gpio_request(pdata->Hook_gpio , NULL);
459 gpio_pull_updown(pdata->Hook_gpio, PullDisable);
460 gpio_direction_input(pdata->Hook_gpio);
461 headset->irq[HOOK] = gpio_to_irq(pdata->Hook_gpio);
462 headset->irq_type[HOOK] = IRQF_TRIGGER_FALLING;
464 ret = request_irq(headset->irq[HOOK], Hook_interrupt, headset->irq_type[HOOK] , "headset_hook", NULL);
467 disable_irq(headset->irq[HOOK]);
469 //------------------------------------------------------------------
470 // Create and register the input driver.
471 headset->input_dev = input_allocate_device();
472 if (!headset->input_dev) {
473 dev_err(&pdev->dev, "failed to allocate input device\n");
477 headset->input_dev->name = pdev->name;
478 headset->input_dev->open = rk_Hskey_open;
479 headset->input_dev->close = rk_Hskey_close;
480 headset->input_dev->dev.parent = &pdev->dev;
481 //input_dev->phys = KEY_PHYS_NAME;
482 headset->input_dev->id.vendor = 0x0001;
483 headset->input_dev->id.product = 0x0001;
484 headset->input_dev->id.version = 0x0100;
485 // Register the input device
486 ret = input_register_device(headset->input_dev);
488 dev_err(&pdev->dev, "failed to register input device\n");
489 goto failed_free_dev;
493 // headset->input_dev->keycode = headset->keycodes;
494 // headset->input_dev->keycodesize = sizeof(unsigned char);
495 // headset->input_dev->keycodemax = 2;
497 // set_bit(KEY_MEDIA, headset->input_dev->keybit);
498 // clear_bit(0, headset->input_dev->keybit);
499 input_set_capability(headset->input_dev, EV_KEY, pdata->hook_key_code);
500 // input_set_capability(headset->input_dev, EV_SW, SW_HEADPHONE_INSERT);
501 // input_set_capability(headset->input_dev, EV_KEY, KEY_END);
503 // headset->input_dev->evbit[0] = BIT_MASK(EV_KEY);
505 headset_info = headset;
506 schedule_delayed_work(&headset->h_delayed_work[HEADSET], msecs_to_jiffies(500));
508 #ifdef CONFIG_HAS_EARLYSUSPEND
509 hs_early_suspend.suspend = NULL;
510 hs_early_suspend.resume = headset_early_resume;
511 hs_early_suspend.level = ~0x0;
512 register_early_suspend(&hs_early_suspend);
518 platform_set_drvdata(pdev, NULL);
519 input_free_device(headset->input_dev);
525 static int rockchip_headsetobserve_suspend(struct platform_device *pdev, pm_message_t state)
527 DBG("%s----%d\n",__FUNCTION__,__LINE__);
528 disable_irq(headset_info->irq[HEADSET]);
529 disable_irq(headset_info->irq[HOOK]);
534 static int rockchip_headsetobserve_resume(struct platform_device *pdev)
536 DBG("%s----%d\n",__FUNCTION__,__LINE__);
537 enable_irq(headset_info->irq[HEADSET]);
538 enable_irq(headset_info->irq[HOOK]);
543 static struct platform_driver rockchip_headsetobserve_driver = {
544 .probe = rockchip_headsetobserve_probe,
545 // .resume = rockchip_headsetobserve_resume,
546 // .suspend = rockchip_headsetobserve_suspend,
548 .name = "rk_headsetdet",
549 .owner = THIS_MODULE,
553 static int __init rockchip_headsetobserve_init(void)
555 platform_driver_register(&rockchip_headsetobserve_driver);
558 module_init(rockchip_headsetobserve_init);
559 MODULE_DESCRIPTION("Rockchip Headset Driver");
560 MODULE_LICENSE("GPL");