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 #if defined(CONFIG_SND_RK_SOC_RK2928) || defined(CONFIG_SND_RK29_SOC_RK610)
65 extern void rk2928_codec_set_spk(bool on);
67 #ifdef CONFIG_SND_SOC_WM8994
68 extern int wm8994_set_status(void);
71 /* headset private data */
73 struct input_dev *input_dev;
74 struct rk_headset_pdata *pdata;
75 unsigned int headset_status:1;
76 unsigned int hook_status:1;
78 unsigned int isHook_irq:1;
79 int cur_headset_status;
82 unsigned int irq_type[2];
83 struct delayed_work h_delayed_work[2];
84 struct switch_dev sdev;
85 struct mutex mutex_lock[2];
86 struct timer_list headset_timer;
87 unsigned char *keycodes;
89 static struct headset_priv *headset_info;
91 #ifdef CONFIG_MODEM_MIC_SWITCH
94 void Modem_Mic_switch(int value)
97 gpio_set_value(headset_info->pdata->Sw_mic_gpio, headset_info->pdata->Hp_mic_io_value);
98 else if(value == MAIN_MIC)
99 gpio_set_value(headset_info->pdata->Sw_mic_gpio,headset_info->pdata->Main_mic_io_value);
101 void Modem_Mic_release(void)
103 if(headset_info->cur_headset_status == 1)
104 gpio_set_value(headset_info->pdata->Sw_mic_gpio, headset_info->pdata->Hp_mic_io_value);
106 gpio_set_value(headset_info->pdata->Sw_mic_gpio,headset_info->pdata->Main_mic_io_value);
109 int Headset_isMic(void)
111 return headset_info->isMic;
113 EXPORT_SYMBOL_GPL(Headset_isMic);
115 int Headset_status(void)
117 if(headset_info->cur_headset_status == BIT_HEADSET_NO_MIC ||
118 headset_info->cur_headset_status == BIT_HEADSET )
123 EXPORT_SYMBOL_GPL(Headset_status);
125 static int read_gpio(int gpio)
130 level = gpio_get_value(gpio);
133 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,gpio,i);
141 printk("%s:get pin level err!\n",__FUNCTION__);
146 static irqreturn_t headset_interrupt(int irq, void *dev_id)
148 DBG("---headset_interrupt---\n");
149 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(50));
153 static irqreturn_t Hook_interrupt(int irq, void *dev_id)
155 DBG("---Hook_interrupt---\n");
156 // disable_irq_nosync(headset_info->irq[HOOK]);
157 schedule_delayed_work(&headset_info->h_delayed_work[HOOK], msecs_to_jiffies(100));
161 static void headsetobserve_work(struct work_struct *work)
165 struct rk_headset_pdata *pdata = headset_info->pdata;
166 static unsigned int old_status = 0;
167 DBG("---headsetobserve_work---\n");
168 mutex_lock(&headset_info->mutex_lock[HEADSET]);
170 level = read_gpio(pdata->Headset_gpio);
174 level2 = read_gpio(pdata->Headset_gpio);
179 old_status = headset_info->headset_status;
180 if(pdata->headset_in_type == HEADSET_IN_HIGH)
181 headset_info->headset_status = level?HEADSET_IN:HEADSET_OUT;
183 headset_info->headset_status = level?HEADSET_OUT:HEADSET_IN;
185 if(old_status == headset_info->headset_status) {
186 DBG("old_status == headset_info->headset_status\n");
190 DBG("(headset in is %s)headset status is %s\n",
191 pdata->headset_in_type?"high level":"low level",
192 headset_info->headset_status?"in":"out");
194 if(headset_info->headset_status == HEADSET_IN)
196 headset_info->cur_headset_status = BIT_HEADSET_NO_MIC;
197 if(pdata->headset_in_type == HEADSET_IN_HIGH)
198 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_FALLING);
200 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_RISING);
201 if (pdata->Hook_gpio) {
202 del_timer(&headset_info->headset_timer);//Start the timer, wait for switch to the headphone channel
203 headset_info->headset_timer.expires = jiffies + 100;
204 add_timer(&headset_info->headset_timer);
208 else if(headset_info->headset_status == HEADSET_OUT)
210 headset_info->hook_status = HOOK_UP;
211 if(headset_info->isHook_irq == enable)
213 DBG("disable headset_hook irq\n");
214 headset_info->isHook_irq = disable;
215 disable_irq(headset_info->irq[HOOK]);
217 headset_info->cur_headset_status = 0;//~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
218 //#if defined(CONFIG_SND_RK_SOC_RK2928) || defined(CONFIG_SOC_RK3028)
219 //rk2928_codec_set_spk(HEADSET_OUT);
221 if(pdata->headset_in_type == HEADSET_IN_HIGH)
222 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_RISING);
224 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_FALLING);
226 rk28_send_wakeup_key();
227 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
228 #if defined(CONFIG_SND_RK_SOC_RK2928) || defined(CONFIG_SND_RK29_SOC_RK610)
229 if (headset_info->headset_status == HEADSET_OUT)
232 rk2928_codec_set_spk(HEADSET_OUT);
233 gpio_set_value(pdata->Sw_mic_gpio, headset_info->pdata->Main_mic_io_value);
236 DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);
239 mutex_unlock(&headset_info->mutex_lock[HEADSET]);
242 static void Hook_work(struct work_struct *work)
245 struct rk_headset_pdata *pdata = headset_info->pdata;
246 static unsigned int old_status = HOOK_UP;
248 mutex_lock(&headset_info->mutex_lock[HOOK]);
250 if(headset_info->headset_status == HEADSET_OUT){
251 DBG("Headset is out\n");
255 #ifdef CONFIG_SND_SOC_WM8994
256 if(wm8994_set_status() != 0) {
257 DBG("wm8994 is not set on heatset channel or suspend\n");
262 level = read_gpio(pdata->Hook_gpio);
266 old_status = headset_info->hook_status;
267 DBG("Hook_work -- level = %d\n",level);
270 headset_info->hook_status = pdata->Hook_down_type == HOOK_DOWN_HIGH?HOOK_UP:HOOK_DOWN;
272 headset_info->hook_status = pdata->Hook_down_type == HOOK_DOWN_HIGH?HOOK_DOWN:HOOK_UP;
274 if(old_status == headset_info->hook_status)
276 DBG("old_status == headset_info->hook_status\n");
279 DBG("Hook_work -- level = %d hook status is %s\n",level,headset_info->hook_status?"key down":"key up");
280 if(headset_info->hook_status == HOOK_DOWN)
282 if(pdata->Hook_down_type == HOOK_DOWN_HIGH)
283 irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_FALLING);
285 irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_RISING);
289 if(pdata->Hook_down_type == HOOK_DOWN_HIGH)
290 irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_RISING);
292 irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_FALLING);
294 input_report_key(headset_info->input_dev,pdata->hook_key_code,headset_info->hook_status);
295 input_sync(headset_info->input_dev);
297 mutex_unlock(&headset_info->mutex_lock[HOOK]);
300 static void headset_timer_callback(unsigned long arg)
302 struct headset_priv *headset = (struct headset_priv *)(arg);
303 struct rk_headset_pdata *pdata = headset->pdata;
306 // printk("headset_timer_callback,headset->headset_status=%d\n",headset->headset_status);
308 if(headset->headset_status == HEADSET_OUT)
310 printk("Headset is out\n");
313 #ifdef CONFIG_SND_SOC_WM8994
314 if(wm8994_set_status() != 0)
316 // printk("wait wm8994 set the MICB2\n");
317 // headset_info->headset_timer.expires = jiffies + 500;
318 headset_info->headset_timer.expires = jiffies + 10;
319 add_timer(&headset_info->headset_timer);
323 level = read_gpio(pdata->Hook_gpio);
326 if((level > 0 && pdata->Hook_down_type == HOOK_DOWN_LOW)
327 || (level == 0 && pdata->Hook_down_type == HOOK_DOWN_HIGH))
329 headset->isMic = 1;//have mic
330 DBG("enable headset_hook irq\n");
331 enable_irq(headset_info->irq[HOOK]);
332 headset->isHook_irq = enable;
333 headset_info->hook_status = HOOK_UP;
334 if(pdata->Hook_down_type == HOOK_DOWN_HIGH)
335 irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_RISING);
337 irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_FALLING);
341 headset->isMic= 0;//No microphone
343 printk("headset->isMic = %d\n",headset->isMic);
344 headset_info->cur_headset_status = headset_info->isMic ? 1:2;//BIT_HEADSET:BIT_HEADSET_NO_MIC;//
345 #if defined(CONFIG_SND_RK_SOC_RK2928) || defined(CONFIG_SND_RK29_SOC_RK610)
346 rk2928_codec_set_spk(HEADSET_IN);
347 if(headset_info->cur_headset_status == 1)
348 gpio_set_value(pdata->Sw_mic_gpio, pdata->Hp_mic_io_value);
350 rk28_send_wakeup_key();
351 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
353 DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);
359 static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
361 return sprintf(buf, "Headset\n");
364 #ifdef CONFIG_HAS_EARLYSUSPEND
365 static void headset_early_resume(struct early_suspend *h)
367 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(10));
368 //DBG(">>>>>headset_early_resume\n");
371 static struct early_suspend hs_early_suspend;
374 static int rk_Hskey_open(struct input_dev *dev)
376 //struct rk28_adckey *adckey = input_get_drvdata(dev);
377 // DBG("===========rk_Hskey_open===========\n");
381 static void rk_Hskey_close(struct input_dev *dev)
383 // DBG("===========rk_Hskey_close===========\n");
384 // struct rk28_adckey *adckey = input_get_drvdata(dev);
388 static int rockchip_headsetobserve_probe(struct platform_device *pdev)
391 struct headset_priv *headset;
392 struct rk_headset_pdata *pdata;
394 headset = kzalloc(sizeof(struct headset_priv), GFP_KERNEL);
395 if (headset == NULL) {
396 dev_err(&pdev->dev, "failed to allocate driver data\n");
399 headset->pdata = pdev->dev.platform_data;
400 pdata = headset->pdata;
401 headset->headset_status = HEADSET_OUT;
402 headset->hook_status = HOOK_UP;
403 headset->isHook_irq = disable;
404 headset->cur_headset_status = 0;
405 headset->sdev.name = "h2w";
406 headset->sdev.print_name = h2w_print_name;
407 ret = switch_dev_register(&headset->sdev);
411 mutex_init(&headset->mutex_lock[HEADSET]);
412 mutex_init(&headset->mutex_lock[HOOK]);
414 INIT_DELAYED_WORK(&headset->h_delayed_work[HEADSET], headsetobserve_work);
415 INIT_DELAYED_WORK(&headset->h_delayed_work[HOOK], Hook_work);
418 setup_timer(&headset->headset_timer, headset_timer_callback, (unsigned long)headset);
419 //------------------------------------------------------------------
420 // Create and register the input driver.
421 headset->input_dev = input_allocate_device();
422 if (!headset->input_dev) {
423 dev_err(&pdev->dev, "failed to allocate input device\n");
427 headset->input_dev->name = pdev->name;
428 headset->input_dev->open = rk_Hskey_open;
429 headset->input_dev->close = rk_Hskey_close;
430 headset->input_dev->dev.parent = &pdev->dev;
431 //input_dev->phys = KEY_PHYS_NAME;
432 headset->input_dev->id.vendor = 0x0001;
433 headset->input_dev->id.product = 0x0001;
434 headset->input_dev->id.version = 0x0100;
435 // Register the input device
436 ret = input_register_device(headset->input_dev);
438 dev_err(&pdev->dev, "failed to register input device\n");
439 goto failed_free_dev;
441 input_set_capability(headset->input_dev, EV_KEY, pdata->hook_key_code);
443 #ifdef CONFIG_HAS_EARLYSUSPEND
444 hs_early_suspend.suspend = NULL;
445 hs_early_suspend.resume = headset_early_resume;
446 hs_early_suspend.level = ~0x0;
447 register_early_suspend(&hs_early_suspend);
449 //------------------------------------------------------------------
450 if (pdata->Headset_gpio) {
451 if(pdata->Headset_gpio == NULL){
452 dev_err(&pdev->dev,"failed init headset,please full hook_io_init function in board\n");
453 goto failed_free_dev;
455 ret = pdata->headset_io_init(pdata->Headset_gpio);
457 goto failed_free_dev;
459 headset->irq[HEADSET] = gpio_to_irq(pdata->Headset_gpio);
461 if(pdata->headset_in_type == HEADSET_IN_HIGH)
462 headset->irq_type[HEADSET] = IRQF_TRIGGER_RISING;
464 headset->irq_type[HEADSET] = IRQF_TRIGGER_FALLING;
465 ret = request_irq(headset->irq[HEADSET], headset_interrupt, headset->irq_type[HEADSET], "headset_input", NULL);
467 goto failed_free_dev;
468 enable_irq_wake(headset->irq[HEADSET]);
471 goto failed_free_dev;
472 //------------------------------------------------------------------
473 if (pdata->Hook_gpio) {
474 if(pdata->hook_io_init == NULL){
475 dev_err(&pdev->dev,"failed init hook,please full hook_io_init function in board\n");
476 goto failed_free_dev;
478 ret = pdata->hook_io_init(pdata->Hook_gpio);
480 goto failed_free_dev;
481 headset->irq[HOOK] = gpio_to_irq(pdata->Hook_gpio);
482 headset->irq_type[HOOK] = pdata->Hook_down_type == HOOK_DOWN_HIGH ? IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
484 ret = request_irq(headset->irq[HOOK], Hook_interrupt, headset->irq_type[HOOK] , "headset_hook", NULL);
486 goto failed_free_dev;
487 disable_irq(headset->irq[HOOK]);
489 //------------------------------------------------------------------
490 headset_info = headset;
491 schedule_delayed_work(&headset->h_delayed_work[HEADSET], msecs_to_jiffies(500));
495 platform_set_drvdata(pdev, NULL);
496 input_free_device(headset->input_dev);
498 dev_err(&pdev->dev, "failed to headset probe\n");
503 static int rockchip_headsetobserve_suspend(struct platform_device *pdev, pm_message_t state)
505 DBG("%s----%d\n",__FUNCTION__,__LINE__);
506 disable_irq(headset_info->irq[HEADSET]);
507 disable_irq(headset_info->irq[HOOK]);
512 static int rockchip_headsetobserve_resume(struct platform_device *pdev)
514 DBG("%s----%d\n",__FUNCTION__,__LINE__);
515 enable_irq(headset_info->irq[HEADSET]);
516 enable_irq(headset_info->irq[HOOK]);
521 static struct platform_driver rockchip_headsetobserve_driver = {
522 .probe = rockchip_headsetobserve_probe,
523 // .resume = rockchip_headsetobserve_resume,
524 // .suspend = rockchip_headsetobserve_suspend,
526 .name = "rk_headsetdet",
527 .owner = THIS_MODULE,
531 static int __init rockchip_headsetobserve_init(void)
533 platform_driver_register(&rockchip_headsetobserve_driver);
536 late_initcall(rockchip_headsetobserve_init);
537 MODULE_DESCRIPTION("Rockchip Headset Driver");
538 MODULE_LICENSE("GPL");