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 del_timer(&headset_info->headset_timer);//Start the timer, wait for switch to the headphone channel
194 headset_info->headset_timer.expires = jiffies + 500;
195 add_timer(&headset_info->headset_timer);
199 // DBG("---HEADSET_IN_HIGH headset out HIGH---\n");
200 if(headset_info->isHook_irq == enable)
202 // DBG("disable_irq\n");
203 headset_info->isHook_irq = disable;
204 disable_irq(headset_info->irq[HOOK]);
206 headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
207 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);//
213 // DBG("---HEADSET_IN_LOW headset in LOW ---\n");
214 headset_info->cur_headset_status = BIT_HEADSET;
215 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);//
216 enable_irq(headset_info->irq[HOOK]);
217 del_timer(&headset_info->headset_timer);//Start the timer, wait for switch to the headphone channel
218 headset_info->headset_timer.expires = jiffies + 500;
219 add_timer(&headset_info->headset_timer);
223 // DBG("---HEADSET_IN_LOW headset out LOW ---\n");
224 if(headset_info->isHook_irq == enable)
226 // DBG("disable_irq\n");
227 headset_info->isHook_irq = disable;
228 disable_irq(headset_info->irq[HOOK]);
230 headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
231 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);//
235 DBG("---- ERROR: on headset headset_in_type error -----\n");
238 rk28_send_wakeup_key();
239 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
240 DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);
242 mutex_unlock(&headset_info->mutex_lock[HEADSET]);
245 static void Hook_work(struct work_struct *work)
248 struct rk_headset_pdata *pdata = headset_info->pdata;
249 static unsigned int old_status = HOOK_UP;
251 // DBG("---Hook_work---\n");
252 mutex_lock(&headset_info->mutex_lock[HOOK]);
253 if(headset_info->headset_status == HEADSET_OUT)
255 DBG("Headset is out\n");
258 #ifdef CONFIG_SND_SOC_WM8994
259 if(wm8994_set_status() < 0)
261 DBG("wm8994 is not set on heatset channel or suspend\n");
267 level = gpio_get_value(pdata->Hook_gpio);
270 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Hook_gpio,i);
279 printk("%s:get pin level err!\n",__FUNCTION__);
283 old_status = headset_info->hook_status;
285 headset_info->hook_status = HOOK_UP;
287 headset_info->hook_status = HOOK_DOWN;
288 if(old_status == headset_info->hook_status)
290 DBG("old_status == headset_info->hook_status\n");
296 DBG("---HOOK Down ---\n");
297 headset_change_irqtype(HOOK,IRQF_TRIGGER_RISING);//
298 input_report_key(headset_info->input_dev,pdata->hook_key_code,headset_info->hook_status);
299 input_sync(headset_info->input_dev);
303 DBG("---HOOK Up ---\n");
304 headset_change_irqtype(HOOK,IRQF_TRIGGER_FALLING);//
305 input_report_key(headset_info->input_dev,pdata->hook_key_code,headset_info->hook_status);
306 input_sync(headset_info->input_dev);
309 mutex_unlock(&headset_info->mutex_lock[HOOK]);
312 static void headset_timer_callback(unsigned long arg)
314 struct headset_priv *headset = (struct headset_priv *)(arg);
315 struct rk_headset_pdata *pdata = headset->pdata;
318 DBG("headset_timer_callback,headset->headset_status=%d\n",headset->headset_status);
320 if(headset->headset_status == HEADSET_OUT)
322 DBG("Headset is out\n");
325 #ifdef CONFIG_SND_SOC_WM8994
326 if(wm8994_set_status() < 0)
328 DBG("wm8994 is not set on heatset channel\n");
329 headset_info->headset_timer.expires = jiffies + 500;
330 add_timer(&headset_info->headset_timer);
336 level = gpio_get_value(pdata->Hook_gpio);
339 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Hook_gpio,i);
348 printk("%s:get pin level err!\n",__FUNCTION__);
353 headset->isMic= 0;//No microphone
356 headset->isMic = 1;//have mic
357 // DBG("enable_irq\n");
358 enable_irq(headset_info->irq[HOOK]);
359 headset->isHook_irq = enable;
361 DBG("headset->isMic = %d\n",headset->isMic);
366 static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
368 return sprintf(buf, "Headset\n");
371 #ifdef CONFIG_HAS_EARLYSUSPEND
372 static void headset_early_resume(struct early_suspend *h)
374 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(10));
375 //DBG(">>>>>headset_early_resume\n");
378 static struct early_suspend hs_early_suspend;
381 static int rk_Hskey_open(struct input_dev *dev)
383 //struct rk28_adckey *adckey = input_get_drvdata(dev);
384 // DBG("===========rk_Hskey_open===========\n");
388 static void rk_Hskey_close(struct input_dev *dev)
390 // DBG("===========rk_Hskey_close===========\n");
391 // struct rk28_adckey *adckey = input_get_drvdata(dev);
395 static int rockchip_headsetobserve_probe(struct platform_device *pdev)
398 struct headset_priv *headset;
399 struct rk_headset_pdata *pdata;
401 headset = kzalloc(sizeof(struct headset_priv), GFP_KERNEL);
402 if (headset == NULL) {
403 dev_err(&pdev->dev, "failed to allocate driver data\n");
406 headset->pdata = pdev->dev.platform_data;
407 pdata = headset->pdata;
408 headset->headset_status = HEADSET_OUT;
409 headset->hook_status = HOOK_UP;
410 headset->isHook_irq = disable;
411 headset->cur_headset_status = 0;
412 headset->sdev.name = "h2w";
413 headset->sdev.print_name = h2w_print_name;
414 ret = switch_dev_register(&headset->sdev);
418 mutex_init(&headset->mutex_lock[HEADSET]);
419 mutex_init(&headset->mutex_lock[HOOK]);
421 INIT_DELAYED_WORK(&headset->h_delayed_work[HEADSET], headsetobserve_work);
422 INIT_DELAYED_WORK(&headset->h_delayed_work[HOOK], Hook_work);
424 // init_timer(&headset->headset_timer);
425 // headset->headset_timer.function = headset_timer_callback;
426 // headset->headset_timer.data = (unsigned long)headset;
427 // headset->headset_timer.expires = jiffies + 3000;
429 setup_timer(&headset->headset_timer, headset_timer_callback, (unsigned long)headset);
430 // headset->headset_timer.expires = jiffies + 1000;
431 // add_timer(&headset->headset_timer);
432 //------------------------------------------------------------------
433 ret = gpio_request(pdata->Headset_gpio, NULL);
436 gpio_pull_updown(pdata->Headset_gpio, PullDisable);
437 gpio_direction_input(pdata->Headset_gpio);
438 headset->irq[HEADSET] = gpio_to_irq(pdata->Headset_gpio);
440 if(pdata->headset_in_type == HEADSET_IN_HIGH)
441 headset->irq_type[HEADSET] = IRQF_TRIGGER_RISING;
443 headset->irq_type[HEADSET] = IRQF_TRIGGER_FALLING;
444 ret = request_irq(headset->irq[HEADSET], headset_interrupt, headset->irq_type[HEADSET], "headset_input", NULL);
447 enable_irq_wake(headset->irq[HEADSET]);
448 //------------------------------------------------------------------
449 ret = gpio_request(pdata->Hook_gpio , NULL);
452 gpio_pull_updown(pdata->Hook_gpio, PullDisable);
453 gpio_direction_input(pdata->Hook_gpio);
454 headset->irq[HOOK] = gpio_to_irq(pdata->Hook_gpio);
455 headset->irq_type[HOOK] = IRQF_TRIGGER_FALLING;
457 ret = request_irq(headset->irq[HOOK], Hook_interrupt, headset->irq_type[HOOK] , "headset_hook", NULL);
460 disable_irq(headset->irq[HOOK]);
461 //------------------------------------------------------------------
462 // Create and register the input driver.
463 headset->input_dev = input_allocate_device();
464 if (!headset->input_dev) {
465 dev_err(&pdev->dev, "failed to allocate input device\n");
469 headset->input_dev->name = pdev->name;
470 headset->input_dev->open = rk_Hskey_open;
471 headset->input_dev->close = rk_Hskey_close;
472 headset->input_dev->dev.parent = &pdev->dev;
473 //input_dev->phys = KEY_PHYS_NAME;
474 headset->input_dev->id.vendor = 0x0001;
475 headset->input_dev->id.product = 0x0001;
476 headset->input_dev->id.version = 0x0100;
477 // Register the input device
478 ret = input_register_device(headset->input_dev);
480 dev_err(&pdev->dev, "failed to register input device\n");
481 goto failed_free_dev;
485 // headset->input_dev->keycode = headset->keycodes;
486 // headset->input_dev->keycodesize = sizeof(unsigned char);
487 // headset->input_dev->keycodemax = 2;
489 // set_bit(KEY_MEDIA, headset->input_dev->keybit);
490 // clear_bit(0, headset->input_dev->keybit);
491 input_set_capability(headset->input_dev, EV_KEY, pdata->hook_key_code);
492 // input_set_capability(headset->input_dev, EV_SW, SW_HEADPHONE_INSERT);
493 // input_set_capability(headset->input_dev, EV_KEY, KEY_END);
495 // headset->input_dev->evbit[0] = BIT_MASK(EV_KEY);
497 headset_info = headset;
498 schedule_delayed_work(&headset->h_delayed_work[HEADSET], msecs_to_jiffies(500));
501 #ifdef CONFIG_HAS_EARLYSUSPEND
502 hs_early_suspend.suspend = NULL;
503 hs_early_suspend.resume = headset_early_resume;
504 hs_early_suspend.level = ~0x0;
505 register_early_suspend(&hs_early_suspend);
511 platform_set_drvdata(pdev, NULL);
512 input_free_device(headset->input_dev);
518 static int rockchip_headsetobserve_suspend(struct platform_device *pdev, pm_message_t state)
520 DBG("%s----%d\n",__FUNCTION__,__LINE__);
521 disable_irq(headset_info->irq[HEADSET]);
522 disable_irq(headset_info->irq[HOOK]);
527 static int rockchip_headsetobserve_resume(struct platform_device *pdev)
529 DBG("%s----%d\n",__FUNCTION__,__LINE__);
530 enable_irq(headset_info->irq[HEADSET]);
531 enable_irq(headset_info->irq[HOOK]);
536 static struct platform_driver rockchip_headsetobserve_driver = {
537 .probe = rockchip_headsetobserve_probe,
538 // .resume = rockchip_headsetobserve_resume,
539 // .suspend = rockchip_headsetobserve_suspend,
541 .name = "rk_headsetdet",
542 .owner = THIS_MODULE,
546 static int __init rockchip_headsetobserve_init(void)
548 platform_driver_register(&rockchip_headsetobserve_driver);
551 module_init(rockchip_headsetobserve_init);
552 MODULE_DESCRIPTION("Rockchip Headset Driver");
553 MODULE_LICENSE("GPL");