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/device.h>
19 #include <linux/interrupt.h>
20 #include <linux/workqueue.h>
21 #include <linux/irq.h>
22 #include <linux/delay.h>
23 #include <linux/types.h>
24 #include <linux/input.h>
25 #include <linux/platform_device.h>
26 #include <linux/mutex.h>
27 #include <linux/errno.h>
28 #include <linux/err.h>
29 #include <linux/hrtimer.h>
30 #include <linux/switch.h>
31 #include <linux/input.h>
32 #include <linux/debugfs.h>
33 #include <linux/wakelock.h>
35 #include <asm/atomic.h>
36 #include <asm/mach-types.h>
38 #include <linux/i2c.h>
39 #include <linux/spi/spi.h>
40 #include "rk_headset.h"
41 #ifdef CONFIG_HAS_EARLYSUSPEND
42 #include <linux/earlysuspend.h>
47 #define DBG(x...) printk(x)
49 #define DBG(x...) do { } while (0)
52 #define BIT_HEADSET (1 << 0)
53 #define BIT_HEADSET_NO_MIC (1 << 1)
65 #if defined(CONFIG_SND_RK_SOC_RK2928) || defined(CONFIG_SND_RK29_SOC_RK610)
66 extern void rk2928_codec_set_spk(bool on);
68 #ifdef CONFIG_SND_SOC_WM8994
69 extern int wm8994_set_status(void);
72 /* headset private data */
74 struct input_dev *input_dev;
75 struct rk_headset_pdata *pdata;
76 unsigned int headset_status:1;
77 unsigned int hook_status:1;
79 unsigned int isHook_irq:1;
80 int cur_headset_status;
83 unsigned int irq_type[2];
84 struct delayed_work h_delayed_work[2];
85 struct switch_dev sdev;
86 struct mutex mutex_lock[2];
87 struct timer_list headset_timer;
88 unsigned char *keycodes;
90 static struct headset_priv *headset_info;
92 #ifdef CONFIG_MODEM_MIC_SWITCH
95 void Modem_Mic_switch(int value)
98 gpio_set_value(headset_info->pdata->mic_switch_gpio, headset_info->pdata->hp_mic_io_value);
99 else if(value == MAIN_MIC)
100 gpio_set_value(headset_info->pdata->mic_switch_gpio,headset_info->pdata->main_mic_io_value);
102 void Modem_Mic_release(void)
104 if(headset_info->cur_headset_status == 1)
105 gpio_set_value(headset_info->pdata->mic_switch_gpio, headset_info->pdata->hp_mic_io_value);
107 gpio_set_value(headset_info->pdata->mic_switch_gpio,headset_info->pdata->main_mic_io_value);
111 static int read_gpio(int gpio)
116 level = gpio_get_value(gpio);
119 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,gpio,i);
127 printk("%s:get pin level err!\n",__FUNCTION__);
132 static irqreturn_t headset_interrupt(int irq, void *dev_id)
134 DBG("---headset_interrupt---\n");
135 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(50));
139 static irqreturn_t hook_interrupt(int irq, void *dev_id)
141 DBG("---Hook_interrupt---\n");
142 // disable_irq_nosync(headset_info->irq[HOOK]);
143 schedule_delayed_work(&headset_info->h_delayed_work[HOOK], msecs_to_jiffies(100));
147 static void headsetobserve_work(struct work_struct *work)
151 struct rk_headset_pdata *pdata = headset_info->pdata;
152 static unsigned int old_status = 0;
153 DBG("---headsetobserve_work---\n");
154 mutex_lock(&headset_info->mutex_lock[HEADSET]);
156 level = read_gpio(pdata->headset_gpio);
160 level2 = read_gpio(pdata->headset_gpio);
165 old_status = headset_info->headset_status;
166 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
167 headset_info->headset_status = level?HEADSET_IN:HEADSET_OUT;
169 headset_info->headset_status = level?HEADSET_OUT:HEADSET_IN;
171 if(old_status == headset_info->headset_status) {
172 DBG("old_status == headset_info->headset_status\n");
176 DBG("(headset in is %s)headset status is %s\n",
177 pdata->headset_insert_type?"high level":"low level",
178 headset_info->headset_status?"in":"out");
180 if(headset_info->headset_status == HEADSET_IN)
182 headset_info->cur_headset_status = BIT_HEADSET_NO_MIC;
183 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
184 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_FALLING);
186 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_RISING);
187 if (pdata->hook_gpio) {
188 del_timer(&headset_info->headset_timer);//Start the timer, wait for switch to the headphone channel
189 headset_info->headset_timer.expires = jiffies + 100;
190 add_timer(&headset_info->headset_timer);
194 else if(headset_info->headset_status == HEADSET_OUT)
196 headset_info->hook_status = HOOK_UP;
197 if(headset_info->isHook_irq == enable)
199 DBG("disable headset_hook irq\n");
200 headset_info->isHook_irq = disable;
201 disable_irq(headset_info->irq[HOOK]);
203 headset_info->cur_headset_status = 0;//~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
204 //#if defined(CONFIG_SND_RK_SOC_RK2928) || defined(CONFIG_SOC_RK3028)
205 //rk2928_codec_set_spk(HEADSET_OUT);
207 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
208 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_RISING);
210 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_FALLING);
212 // rk28_send_wakeup_key();
213 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
214 #if defined(CONFIG_SND_RK_SOC_RK2928) || defined(CONFIG_SND_RK29_SOC_RK610)
215 if (headset_info->headset_status == HEADSET_OUT)
218 rk2928_codec_set_spk(HEADSET_OUT);
219 gpio_set_value(pdata->Sw_mic_gpio, headset_info->pdata->Main_mic_io_value);
222 DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);
225 mutex_unlock(&headset_info->mutex_lock[HEADSET]);
228 static void hook_work(struct work_struct *work)
231 struct rk_headset_pdata *pdata = headset_info->pdata;
232 static unsigned int old_status = HOOK_UP;
234 mutex_lock(&headset_info->mutex_lock[HOOK]);
236 if(headset_info->headset_status == HEADSET_OUT){
237 DBG("Headset is out\n");
241 #ifdef CONFIG_SND_SOC_WM8994
242 if(wm8994_set_status() != 0) {
243 DBG("wm8994 is not set on heatset channel or suspend\n");
248 level = read_gpio(pdata->hook_gpio);
252 old_status = headset_info->hook_status;
253 DBG("Hook_work -- level = %d\n",level);
256 headset_info->hook_status = pdata->hook_down_type == HOOK_DOWN_HIGH?HOOK_UP:HOOK_DOWN;
258 headset_info->hook_status = pdata->hook_down_type == HOOK_DOWN_HIGH?HOOK_DOWN:HOOK_UP;
260 if(old_status == headset_info->hook_status)
262 DBG("old_status == headset_info->hook_status\n");
265 DBG("Hook_work -- level = %d hook status is %s\n",level,headset_info->hook_status?"key down":"key up");
266 if(headset_info->hook_status == HOOK_DOWN)
268 if(pdata->hook_down_type == HOOK_DOWN_HIGH)
269 irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_FALLING);
271 irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_RISING);
275 if(pdata->hook_down_type == HOOK_DOWN_HIGH)
276 irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_RISING);
278 irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_FALLING);
280 input_report_key(headset_info->input_dev,HOOK_KEY_CODE,headset_info->hook_status);
281 input_sync(headset_info->input_dev);
283 mutex_unlock(&headset_info->mutex_lock[HOOK]);
286 static void headset_timer_callback(unsigned long arg)
288 struct headset_priv *headset = (struct headset_priv *)(arg);
289 struct rk_headset_pdata *pdata = headset->pdata;
292 // printk("headset_timer_callback,headset->headset_status=%d\n",headset->headset_status);
294 if(headset->headset_status == HEADSET_OUT)
296 printk("Headset is out\n");
299 #ifdef CONFIG_SND_SOC_WM8994
300 if(wm8994_set_status() != 0)
302 // printk("wait wm8994 set the MICB2\n");
303 // headset_info->headset_timer.expires = jiffies + 500;
304 headset_info->headset_timer.expires = jiffies + 10;
305 add_timer(&headset_info->headset_timer);
309 level = read_gpio(pdata->hook_gpio);
312 if((level > 0 && pdata->hook_down_type == HOOK_DOWN_LOW)
313 || (level == 0 && pdata->hook_down_type == HOOK_DOWN_HIGH))
315 headset->isMic = 1;//have mic
316 DBG("enable headset_hook irq\n");
317 enable_irq(headset_info->irq[HOOK]);
318 headset->isHook_irq = enable;
319 headset_info->hook_status = HOOK_UP;
320 if(pdata->hook_down_type == HOOK_DOWN_HIGH)
321 irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_RISING);
323 irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_FALLING);
327 headset->isMic= 0;//No microphone
329 printk("headset->isMic = %d\n",headset->isMic);
330 headset_info->cur_headset_status = headset_info->isMic ? 1:2;//BIT_HEADSET:BIT_HEADSET_NO_MIC;//
331 #if defined(CONFIG_SND_RK_SOC_RK2928) || defined(CONFIG_SND_RK29_SOC_RK610)
332 rk2928_codec_set_spk(HEADSET_IN);
333 if(headset_info->cur_headset_status == 1)
334 gpio_set_value(pdata->Sw_mic_gpio, pdata->Hp_mic_io_value);
336 // rk28_send_wakeup_key();
337 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
339 DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);
345 static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
347 return sprintf(buf, "Headset\n");
350 #ifdef CONFIG_HAS_EARLYSUSPEND
351 static void headset_early_resume(struct early_suspend *h)
353 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(10));
354 //DBG(">>>>>headset_early_resume\n");
357 static struct early_suspend hs_early_suspend;
360 static int rk_hskey_open(struct input_dev *dev)
362 //struct rk28_adckey *adckey = input_get_drvdata(dev);
363 // DBG("===========rk_Hskey_open===========\n");
367 static void rk_hskey_close(struct input_dev *dev)
369 // DBG("===========rk_Hskey_close===========\n");
370 // struct rk28_adckey *adckey = input_get_drvdata(dev);
374 int rk_headset_probe(struct platform_device *pdev,struct rk_headset_pdata *pdata)
377 struct headset_priv *headset;
379 headset = kzalloc(sizeof(struct headset_priv), GFP_KERNEL);
380 if (headset == NULL) {
381 dev_err(&pdev->dev, "failed to allocate driver data\n");
385 headset->pdata = pdata;
386 headset->headset_status = HEADSET_OUT;
387 headset->hook_status = HOOK_UP;
388 headset->isHook_irq = disable;
389 headset->cur_headset_status = 0;
390 headset->sdev.name = "h2w";
391 headset->sdev.print_name = h2w_print_name;
392 ret = switch_dev_register(&headset->sdev);
396 mutex_init(&headset->mutex_lock[HEADSET]);
397 mutex_init(&headset->mutex_lock[HOOK]);
399 INIT_DELAYED_WORK(&headset->h_delayed_work[HEADSET], headsetobserve_work);
400 INIT_DELAYED_WORK(&headset->h_delayed_work[HOOK], hook_work);
403 setup_timer(&headset->headset_timer, headset_timer_callback, (unsigned long)headset);
404 //------------------------------------------------------------------
405 // Create and register the input driver.
406 headset->input_dev = input_allocate_device();
407 if (!headset->input_dev) {
408 dev_err(&pdev->dev, "failed to allocate input device\n");
412 headset->input_dev->name = pdev->name;
413 headset->input_dev->open = rk_hskey_open;
414 headset->input_dev->close = rk_hskey_close;
415 headset->input_dev->dev.parent = &pdev->dev;
416 //input_dev->phys = KEY_PHYS_NAME;
417 headset->input_dev->id.vendor = 0x0001;
418 headset->input_dev->id.product = 0x0001;
419 headset->input_dev->id.version = 0x0100;
420 // Register the input device
421 ret = input_register_device(headset->input_dev);
423 dev_err(&pdev->dev, "failed to register input device\n");
424 goto failed_free_dev;
426 input_set_capability(headset->input_dev, EV_KEY,HOOK_KEY_CODE);
428 #ifdef CONFIG_HAS_EARLYSUSPEND
429 hs_early_suspend.suspend = NULL;
430 hs_early_suspend.resume = headset_early_resume;
431 hs_early_suspend.level = ~0x0;
432 register_early_suspend(&hs_early_suspend);
434 //------------------------------------------------------------------
435 if (pdata->headset_gpio) {
436 if(!pdata->headset_gpio){
437 dev_err(&pdev->dev,"failed init headset,please full hook_io_init function in board\n");
438 goto failed_free_dev;
441 headset->irq[HEADSET] = gpio_to_irq(pdata->headset_gpio);
443 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
444 headset->irq_type[HEADSET] = IRQF_TRIGGER_RISING;
446 headset->irq_type[HEADSET] = IRQF_TRIGGER_FALLING;
447 ret = request_irq(headset->irq[HEADSET], headset_interrupt, headset->irq_type[HEADSET], "headset_input", NULL);
449 goto failed_free_dev;
450 enable_irq_wake(headset->irq[HEADSET]);
453 goto failed_free_dev;
454 //------------------------------------------------------------------
455 if (pdata->hook_gpio) {
456 headset->irq[HOOK] = gpio_to_irq(pdata->hook_gpio);
457 headset->irq_type[HOOK] = pdata->hook_down_type == HOOK_DOWN_HIGH ? IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
459 ret = request_irq(headset->irq[HOOK], hook_interrupt, headset->irq_type[HOOK] , "headset_hook", NULL);
461 goto failed_free_dev;
462 disable_irq(headset->irq[HOOK]);
464 //------------------------------------------------------------------
465 headset_info = headset;
466 schedule_delayed_work(&headset->h_delayed_work[HEADSET], msecs_to_jiffies(500));
470 platform_set_drvdata(pdev, NULL);
471 input_free_device(headset->input_dev);
473 dev_err(&pdev->dev, "failed to headset probe\n");