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 #ifdef CONFIG_SND_RK_SOC_RK2928
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 int Headset_isMic(void)
93 return headset_info->isMic;
95 EXPORT_SYMBOL_GPL(Headset_isMic);
97 int Headset_status(void)
99 if(headset_info->cur_headset_status == BIT_HEADSET_NO_MIC ||
100 headset_info->cur_headset_status == BIT_HEADSET )
105 EXPORT_SYMBOL_GPL(Headset_status);
107 static int read_gpio(int gpio)
112 level = gpio_get_value(gpio);
115 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,gpio,i);
123 printk("%s:get pin level err!\n",__FUNCTION__);
128 static irqreturn_t headset_interrupt(int irq, void *dev_id)
130 DBG("---headset_interrupt---\n");
131 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(50));
135 static irqreturn_t Hook_interrupt(int irq, void *dev_id)
137 DBG("---Hook_interrupt---\n");
138 // disable_irq_nosync(headset_info->irq[HOOK]);
139 schedule_delayed_work(&headset_info->h_delayed_work[HOOK], msecs_to_jiffies(50));
143 static void headsetobserve_work(struct work_struct *work)
146 struct rk_headset_pdata *pdata = headset_info->pdata;
147 static unsigned int old_status = 0;
148 DBG("---headsetobserve_work---\n");
149 mutex_lock(&headset_info->mutex_lock[HEADSET]);
151 level = read_gpio(pdata->Headset_gpio);
155 old_status = headset_info->headset_status;
156 if(pdata->headset_in_type == HEADSET_IN_HIGH)
157 headset_info->headset_status = level?HEADSET_IN:HEADSET_OUT;
159 headset_info->headset_status = level?HEADSET_OUT:HEADSET_IN;
161 if(old_status == headset_info->headset_status) {
162 DBG("old_status == headset_info->headset_status\n");
166 DBG("(headset in is %s)headset status is %s\n",
167 pdata->headset_in_type?"high level":"low level",
168 headset_info->headset_status?"in":"out");
170 if(headset_info->headset_status == HEADSET_IN)
172 headset_info->cur_headset_status = BIT_HEADSET_NO_MIC;
173 if(pdata->headset_in_type == HEADSET_IN_HIGH)
174 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_FALLING);
176 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_RISING);
177 if (pdata->Hook_gpio) {
178 del_timer(&headset_info->headset_timer);//Start the timer, wait for switch to the headphone channel
179 headset_info->headset_timer.expires = jiffies + 10;
180 add_timer(&headset_info->headset_timer);
184 else if(headset_info->headset_status == HEADSET_OUT)
186 headset_info->hook_status = HOOK_UP;
187 if(headset_info->isHook_irq == enable)
189 DBG("disable headset_hook irq\n");
190 headset_info->isHook_irq = disable;
191 disable_irq(headset_info->irq[HOOK]);
193 headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
194 #ifdef CONFIG_SND_RK_SOC_RK2928
195 rk2928_codec_set_spk(HEADSET_OUT);
197 if(pdata->headset_in_type == HEADSET_IN_HIGH)
198 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_RISING);
200 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_FALLING);
202 rk28_send_wakeup_key();
203 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
204 DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);
207 mutex_unlock(&headset_info->mutex_lock[HEADSET]);
210 static void Hook_work(struct work_struct *work)
213 struct rk_headset_pdata *pdata = headset_info->pdata;
214 static unsigned int old_status = HOOK_UP;
216 mutex_lock(&headset_info->mutex_lock[HOOK]);
218 if(headset_info->headset_status == HEADSET_OUT){
219 DBG("Headset is out\n");
223 #ifdef CONFIG_SND_SOC_WM8994
224 if(wm8994_set_status() != 0) {
225 DBG("wm8994 is not set on heatset channel or suspend\n");
230 level = read_gpio(pdata->Hook_gpio);
234 old_status = headset_info->hook_status;
235 // DBG("Hook_work -- level = %d\n",level);
238 headset_info->hook_status = pdata->Hook_down_type == HOOK_DOWN_HIGH?HOOK_UP:HOOK_DOWN;
240 headset_info->hook_status = pdata->Hook_down_type == HOOK_DOWN_HIGH?HOOK_DOWN:HOOK_UP;
242 if(old_status == headset_info->hook_status)
244 DBG("old_status == headset_info->hook_status\n");
247 DBG("Hook_work -- level = %d hook status is %s\n",level,headset_info->hook_status?"key down":"key dup");
248 if(headset_info->hook_status == HOOK_DOWN)
250 if(pdata->Hook_down_type == HOOK_DOWN_HIGH)
251 irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_FALLING);
253 irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_RISING);
257 if(pdata->Hook_down_type == HOOK_DOWN_HIGH)
258 irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_RISING);
260 irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_FALLING);
262 input_report_key(headset_info->input_dev,pdata->hook_key_code,headset_info->hook_status);
263 input_sync(headset_info->input_dev);
265 mutex_unlock(&headset_info->mutex_lock[HOOK]);
268 static void headset_timer_callback(unsigned long arg)
270 struct headset_priv *headset = (struct headset_priv *)(arg);
271 struct rk_headset_pdata *pdata = headset->pdata;
274 // printk("headset_timer_callback,headset->headset_status=%d\n",headset->headset_status);
276 if(headset->headset_status == HEADSET_OUT)
278 printk("Headset is out\n");
281 #ifdef CONFIG_SND_SOC_WM8994
282 if(wm8994_set_status() != 0)
284 // printk("wait wm8994 set the MICB2\n");
285 // headset_info->headset_timer.expires = jiffies + 500;
286 headset_info->headset_timer.expires = jiffies + 10;
287 add_timer(&headset_info->headset_timer);
291 level = read_gpio(pdata->Hook_gpio);
295 if((level > 0 && pdata->Hook_down_type == HOOK_DOWN_LOW)
296 || (level == 0 && pdata->Hook_down_type == HOOK_DOWN_HIGH))
298 headset->isMic = 1;//have mic
299 DBG("enable headset_hook irq\n");
300 enable_irq(headset_info->irq[HOOK]);
301 headset->isHook_irq = enable;
304 headset->isMic= 0;//No microphone
306 printk("headset->isMic = %d\n",headset->isMic);
307 headset_info->cur_headset_status = headset_info->isMic ? BIT_HEADSET:BIT_HEADSET_NO_MIC;
308 #ifdef CONFIG_SND_RK_SOC_RK2928
309 rk2928_codec_set_spk(HEADSET_IN);
311 rk28_send_wakeup_key();
312 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
313 DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);
319 static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
321 return sprintf(buf, "Headset\n");
324 #ifdef CONFIG_HAS_EARLYSUSPEND
325 static void headset_early_resume(struct early_suspend *h)
327 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(10));
328 //DBG(">>>>>headset_early_resume\n");
331 static struct early_suspend hs_early_suspend;
334 static int rk_Hskey_open(struct input_dev *dev)
336 //struct rk28_adckey *adckey = input_get_drvdata(dev);
337 // DBG("===========rk_Hskey_open===========\n");
341 static void rk_Hskey_close(struct input_dev *dev)
343 // DBG("===========rk_Hskey_close===========\n");
344 // struct rk28_adckey *adckey = input_get_drvdata(dev);
348 static int rockchip_headsetobserve_probe(struct platform_device *pdev)
351 struct headset_priv *headset;
352 struct rk_headset_pdata *pdata;
354 headset = kzalloc(sizeof(struct headset_priv), GFP_KERNEL);
355 if (headset == NULL) {
356 dev_err(&pdev->dev, "failed to allocate driver data\n");
359 headset->pdata = pdev->dev.platform_data;
360 pdata = headset->pdata;
361 headset->headset_status = HEADSET_OUT;
362 headset->hook_status = HOOK_UP;
363 headset->isHook_irq = disable;
364 headset->cur_headset_status = 0;
365 headset->sdev.name = "h2w";
366 headset->sdev.print_name = h2w_print_name;
367 ret = switch_dev_register(&headset->sdev);
371 mutex_init(&headset->mutex_lock[HEADSET]);
372 mutex_init(&headset->mutex_lock[HOOK]);
374 INIT_DELAYED_WORK(&headset->h_delayed_work[HEADSET], headsetobserve_work);
375 INIT_DELAYED_WORK(&headset->h_delayed_work[HOOK], Hook_work);
378 setup_timer(&headset->headset_timer, headset_timer_callback, (unsigned long)headset);
379 //------------------------------------------------------------------
380 // Create and register the input driver.
381 headset->input_dev = input_allocate_device();
382 if (!headset->input_dev) {
383 dev_err(&pdev->dev, "failed to allocate input device\n");
387 headset->input_dev->name = pdev->name;
388 headset->input_dev->open = rk_Hskey_open;
389 headset->input_dev->close = rk_Hskey_close;
390 headset->input_dev->dev.parent = &pdev->dev;
391 //input_dev->phys = KEY_PHYS_NAME;
392 headset->input_dev->id.vendor = 0x0001;
393 headset->input_dev->id.product = 0x0001;
394 headset->input_dev->id.version = 0x0100;
395 // Register the input device
396 ret = input_register_device(headset->input_dev);
398 dev_err(&pdev->dev, "failed to register input device\n");
399 goto failed_free_dev;
401 input_set_capability(headset->input_dev, EV_KEY, pdata->hook_key_code);
403 #ifdef CONFIG_HAS_EARLYSUSPEND
404 hs_early_suspend.suspend = NULL;
405 hs_early_suspend.resume = headset_early_resume;
406 hs_early_suspend.level = ~0x0;
407 register_early_suspend(&hs_early_suspend);
409 //------------------------------------------------------------------
410 if (pdata->Headset_gpio) {
411 ret = pdata->headset_io_init(pdata->Headset_gpio, pdata->headset_gpio_info.iomux_name, pdata->headset_gpio_info.iomux_mode);
415 headset->irq[HEADSET] = gpio_to_irq(pdata->Headset_gpio);
417 if(pdata->headset_in_type == HEADSET_IN_HIGH)
418 headset->irq_type[HEADSET] = IRQF_TRIGGER_RISING;
420 headset->irq_type[HEADSET] = IRQF_TRIGGER_FALLING;
421 ret = request_irq(headset->irq[HEADSET], headset_interrupt, headset->irq_type[HEADSET], "headset_input", NULL);
423 goto failed_free_dev;
424 enable_irq_wake(headset->irq[HEADSET]);
427 goto failed_free_dev;
428 //------------------------------------------------------------------
429 if (pdata->Hook_gpio) {
430 ret = pdata->hook_io_init(pdata->Hook_gpio, pdata->hook_gpio_info.iomux_name, pdata->hook_gpio_info.iomux_mode);
433 headset->irq[HOOK] = gpio_to_irq(pdata->Hook_gpio);
434 headset->irq_type[HOOK] = pdata->Hook_down_type == HOOK_DOWN_HIGH ? IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
436 ret = request_irq(headset->irq[HOOK], Hook_interrupt, headset->irq_type[HOOK] , "headset_hook", NULL);
438 goto failed_free_dev;
439 disable_irq(headset->irq[HOOK]);
441 //------------------------------------------------------------------
442 headset_info = headset;
443 schedule_delayed_work(&headset->h_delayed_work[HEADSET], msecs_to_jiffies(500));
447 platform_set_drvdata(pdev, NULL);
448 input_free_device(headset->input_dev);
450 dev_err(&pdev->dev, "failed to headset probe\n");
455 static int rockchip_headsetobserve_suspend(struct platform_device *pdev, pm_message_t state)
457 DBG("%s----%d\n",__FUNCTION__,__LINE__);
458 disable_irq(headset_info->irq[HEADSET]);
459 disable_irq(headset_info->irq[HOOK]);
464 static int rockchip_headsetobserve_resume(struct platform_device *pdev)
466 DBG("%s----%d\n",__FUNCTION__,__LINE__);
467 enable_irq(headset_info->irq[HEADSET]);
468 enable_irq(headset_info->irq[HOOK]);
473 static struct platform_driver rockchip_headsetobserve_driver = {
474 .probe = rockchip_headsetobserve_probe,
475 // .resume = rockchip_headsetobserve_resume,
476 // .suspend = rockchip_headsetobserve_suspend,
478 .name = "rk_headsetdet",
479 .owner = THIS_MODULE,
483 static int __init rockchip_headsetobserve_init(void)
485 platform_driver_register(&rockchip_headsetobserve_driver);
488 late_initcall(rockchip_headsetobserve_init);
489 MODULE_DESCRIPTION("Rockchip Headset Driver");
490 MODULE_LICENSE("GPL");