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/debugfs.h>
32 #include <linux/wakelock.h>
33 #include <linux/gpio.h>
34 #include <asm/atomic.h>
36 #include <linux/i2c.h>
37 #include <linux/spi/spi.h>
38 #include "rk_headset.h"
39 #ifdef CONFIG_HAS_EARLYSUSPEND
40 #include <linux/earlysuspend.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 #if defined(CONFIG_SND_RK_SOC_RK2928) || defined(CONFIG_SND_RK29_SOC_RK610)
64 extern void rk2928_codec_set_spk(bool on);
66 #ifdef CONFIG_SND_SOC_WM8994
67 extern int wm8994_set_status(void);
70 /* headset private data */
72 struct input_dev *input_dev;
73 struct rk_headset_pdata *pdata;
74 unsigned int headset_status:1;
75 unsigned int hook_status:1;
77 unsigned int isHook_irq:1;
78 int cur_headset_status;
81 unsigned int irq_type[2];
82 struct delayed_work h_delayed_work[2];
83 struct switch_dev sdev;
84 struct mutex mutex_lock[2];
85 struct timer_list headset_timer;
86 unsigned char *keycodes;
88 static struct headset_priv *headset_info;
90 #ifdef CONFIG_MODEM_MIC_SWITCH
93 void Modem_Mic_switch(int value)
96 gpio_set_value(headset_info->pdata->mic_switch_gpio, headset_info->pdata->hp_mic_io_value);
97 else if(value == MAIN_MIC)
98 gpio_set_value(headset_info->pdata->mic_switch_gpio,headset_info->pdata->main_mic_io_value);
100 void Modem_Mic_release(void)
102 if(headset_info->cur_headset_status == 1)
103 gpio_set_value(headset_info->pdata->mic_switch_gpio, headset_info->pdata->hp_mic_io_value);
105 gpio_set_value(headset_info->pdata->mic_switch_gpio,headset_info->pdata->main_mic_io_value);
109 static int read_gpio(int gpio)
114 level = gpio_get_value(gpio);
117 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,gpio,i);
125 printk("%s:get pin level err!\n",__FUNCTION__);
130 static irqreturn_t headset_interrupt(int irq, void *dev_id)
132 DBG("---headset_interrupt---\n");
133 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(50));
137 static irqreturn_t hook_interrupt(int irq, void *dev_id)
139 DBG("---Hook_interrupt---\n");
140 // disable_irq_nosync(headset_info->irq[HOOK]);
141 schedule_delayed_work(&headset_info->h_delayed_work[HOOK], msecs_to_jiffies(100));
145 static void headsetobserve_work(struct work_struct *work)
149 struct rk_headset_pdata *pdata = headset_info->pdata;
150 static unsigned int old_status = 0;
151 DBG("---headsetobserve_work---\n");
152 mutex_lock(&headset_info->mutex_lock[HEADSET]);
154 level = read_gpio(pdata->headset_gpio);
158 level2 = read_gpio(pdata->headset_gpio);
163 old_status = headset_info->headset_status;
164 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
165 headset_info->headset_status = level?HEADSET_IN:HEADSET_OUT;
167 headset_info->headset_status = level?HEADSET_OUT:HEADSET_IN;
169 if(old_status == headset_info->headset_status) {
170 DBG("old_status == headset_info->headset_status\n");
174 DBG("(headset in is %s)headset status is %s\n",
175 pdata->headset_insert_type?"high level":"low level",
176 headset_info->headset_status?"in":"out");
178 if(headset_info->headset_status == HEADSET_IN)
180 headset_info->cur_headset_status = BIT_HEADSET_NO_MIC;
181 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
182 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_FALLING);
184 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_RISING);
185 if (pdata->hook_gpio) {
186 del_timer(&headset_info->headset_timer);//Start the timer, wait for switch to the headphone channel
187 headset_info->headset_timer.expires = jiffies + 100;
188 add_timer(&headset_info->headset_timer);
192 else if(headset_info->headset_status == HEADSET_OUT)
194 headset_info->hook_status = HOOK_UP;
195 if(headset_info->isHook_irq == enable)
197 DBG("disable headset_hook irq\n");
198 headset_info->isHook_irq = disable;
199 disable_irq(headset_info->irq[HOOK]);
201 headset_info->cur_headset_status = 0;//~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
202 //#if defined(CONFIG_SND_RK_SOC_RK2928) || defined(CONFIG_SOC_RK3028)
203 //rk2928_codec_set_spk(HEADSET_OUT);
205 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
206 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_RISING);
208 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_FALLING);
210 // rk28_send_wakeup_key();
211 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
212 #if defined(CONFIG_SND_RK_SOC_RK2928) || defined(CONFIG_SND_RK29_SOC_RK610)
213 if (headset_info->headset_status == HEADSET_OUT)
216 rk2928_codec_set_spk(HEADSET_OUT);
217 gpio_set_value(pdata->Sw_mic_gpio, headset_info->pdata->Main_mic_io_value);
220 DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);
223 mutex_unlock(&headset_info->mutex_lock[HEADSET]);
226 static void hook_work(struct work_struct *work)
229 struct rk_headset_pdata *pdata = headset_info->pdata;
230 static unsigned int old_status = HOOK_UP;
232 mutex_lock(&headset_info->mutex_lock[HOOK]);
234 if(headset_info->headset_status == HEADSET_OUT){
235 DBG("Headset is out\n");
239 #ifdef CONFIG_SND_SOC_WM8994
240 if(wm8994_set_status() != 0) {
241 DBG("wm8994 is not set on heatset channel or suspend\n");
246 level = read_gpio(pdata->hook_gpio);
250 old_status = headset_info->hook_status;
251 DBG("Hook_work -- level = %d\n",level);
254 headset_info->hook_status = pdata->hook_down_type == HOOK_DOWN_HIGH?HOOK_UP:HOOK_DOWN;
256 headset_info->hook_status = pdata->hook_down_type == HOOK_DOWN_HIGH?HOOK_DOWN:HOOK_UP;
258 if(old_status == headset_info->hook_status)
260 DBG("old_status == headset_info->hook_status\n");
263 DBG("Hook_work -- level = %d hook status is %s\n",level,headset_info->hook_status?"key down":"key up");
264 if(headset_info->hook_status == HOOK_DOWN)
266 if(pdata->hook_down_type == HOOK_DOWN_HIGH)
267 irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_FALLING);
269 irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_RISING);
273 if(pdata->hook_down_type == HOOK_DOWN_HIGH)
274 irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_RISING);
276 irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_FALLING);
278 input_report_key(headset_info->input_dev,HOOK_KEY_CODE,headset_info->hook_status);
279 input_sync(headset_info->input_dev);
281 mutex_unlock(&headset_info->mutex_lock[HOOK]);
284 static void headset_timer_callback(unsigned long arg)
286 struct headset_priv *headset = (struct headset_priv *)(arg);
287 struct rk_headset_pdata *pdata = headset->pdata;
290 // printk("headset_timer_callback,headset->headset_status=%d\n",headset->headset_status);
292 if(headset->headset_status == HEADSET_OUT)
294 printk("Headset is out\n");
297 #ifdef CONFIG_SND_SOC_WM8994
298 if(wm8994_set_status() != 0)
300 // printk("wait wm8994 set the MICB2\n");
301 // headset_info->headset_timer.expires = jiffies + 500;
302 headset_info->headset_timer.expires = jiffies + 10;
303 add_timer(&headset_info->headset_timer);
307 level = read_gpio(pdata->hook_gpio);
310 if((level > 0 && pdata->hook_down_type == HOOK_DOWN_LOW)
311 || (level == 0 && pdata->hook_down_type == HOOK_DOWN_HIGH))
313 headset->isMic = 1;//have mic
314 DBG("enable headset_hook irq\n");
315 enable_irq(headset_info->irq[HOOK]);
316 headset->isHook_irq = enable;
317 headset_info->hook_status = HOOK_UP;
318 if(pdata->hook_down_type == HOOK_DOWN_HIGH)
319 irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_RISING);
321 irq_set_irq_type(headset_info->irq[HOOK],IRQF_TRIGGER_FALLING);
325 headset->isMic= 0;//No microphone
327 printk("headset->isMic = %d\n",headset->isMic);
328 headset_info->cur_headset_status = headset_info->isMic ? 1:2;//BIT_HEADSET:BIT_HEADSET_NO_MIC;//
329 #if defined(CONFIG_SND_RK_SOC_RK2928) || defined(CONFIG_SND_RK29_SOC_RK610)
330 rk2928_codec_set_spk(HEADSET_IN);
331 if(headset_info->cur_headset_status == 1)
332 gpio_set_value(pdata->Sw_mic_gpio, pdata->Hp_mic_io_value);
334 // rk28_send_wakeup_key();
335 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
337 DBG("headset_info->cur_headset_status = %d\n",headset_info->cur_headset_status);
343 static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
345 return sprintf(buf, "Headset\n");
348 #ifdef CONFIG_HAS_EARLYSUSPEND
349 static void headset_early_resume(struct early_suspend *h)
351 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(10));
352 //DBG(">>>>>headset_early_resume\n");
355 static struct early_suspend hs_early_suspend;
358 static int rk_hskey_open(struct input_dev *dev)
360 //struct rk28_adckey *adckey = input_get_drvdata(dev);
361 // DBG("===========rk_Hskey_open===========\n");
365 static void rk_hskey_close(struct input_dev *dev)
367 // DBG("===========rk_Hskey_close===========\n");
368 // struct rk28_adckey *adckey = input_get_drvdata(dev);
372 int rk_headset_probe(struct platform_device *pdev,struct rk_headset_pdata *pdata)
375 struct headset_priv *headset;
377 headset = kzalloc(sizeof(struct headset_priv), GFP_KERNEL);
378 if (headset == NULL) {
379 dev_err(&pdev->dev, "failed to allocate driver data\n");
383 headset->pdata = pdata;
384 headset->headset_status = HEADSET_OUT;
385 headset->hook_status = HOOK_UP;
386 headset->isHook_irq = disable;
387 headset->cur_headset_status = 0;
388 headset->sdev.name = "h2w";
389 headset->sdev.print_name = h2w_print_name;
390 ret = switch_dev_register(&headset->sdev);
394 mutex_init(&headset->mutex_lock[HEADSET]);
395 mutex_init(&headset->mutex_lock[HOOK]);
397 INIT_DELAYED_WORK(&headset->h_delayed_work[HEADSET], headsetobserve_work);
398 INIT_DELAYED_WORK(&headset->h_delayed_work[HOOK], hook_work);
401 setup_timer(&headset->headset_timer, headset_timer_callback, (unsigned long)headset);
402 //------------------------------------------------------------------
403 // Create and register the input driver.
404 headset->input_dev = input_allocate_device();
405 if (!headset->input_dev) {
406 dev_err(&pdev->dev, "failed to allocate input device\n");
410 headset->input_dev->name = pdev->name;
411 headset->input_dev->open = rk_hskey_open;
412 headset->input_dev->close = rk_hskey_close;
413 headset->input_dev->dev.parent = &pdev->dev;
414 //input_dev->phys = KEY_PHYS_NAME;
415 headset->input_dev->id.vendor = 0x0001;
416 headset->input_dev->id.product = 0x0001;
417 headset->input_dev->id.version = 0x0100;
418 // Register the input device
419 ret = input_register_device(headset->input_dev);
421 dev_err(&pdev->dev, "failed to register input device\n");
422 goto failed_free_dev;
424 input_set_capability(headset->input_dev, EV_KEY,HOOK_KEY_CODE);
426 #ifdef CONFIG_HAS_EARLYSUSPEND
427 hs_early_suspend.suspend = NULL;
428 hs_early_suspend.resume = headset_early_resume;
429 hs_early_suspend.level = ~0x0;
430 register_early_suspend(&hs_early_suspend);
432 //------------------------------------------------------------------
433 if (pdata->headset_gpio) {
434 if(!pdata->headset_gpio){
435 dev_err(&pdev->dev,"failed init headset,please full hook_io_init function in board\n");
436 goto failed_free_dev;
439 headset->irq[HEADSET] = gpio_to_irq(pdata->headset_gpio);
441 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
442 headset->irq_type[HEADSET] = IRQF_TRIGGER_RISING;
444 headset->irq_type[HEADSET] = IRQF_TRIGGER_FALLING;
445 ret = request_irq(headset->irq[HEADSET], headset_interrupt, headset->irq_type[HEADSET], "headset_input", NULL);
447 goto failed_free_dev;
448 if (pdata->headset_wakeup)
449 enable_irq_wake(headset->irq[HEADSET]);
452 goto failed_free_dev;
453 //------------------------------------------------------------------
454 if (pdata->hook_gpio) {
455 headset->irq[HOOK] = gpio_to_irq(pdata->hook_gpio);
456 headset->irq_type[HOOK] = pdata->hook_down_type == HOOK_DOWN_HIGH ? IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
458 ret = request_irq(headset->irq[HOOK], hook_interrupt, headset->irq_type[HOOK] , "headset_hook", NULL);
460 goto failed_free_dev;
461 disable_irq(headset->irq[HOOK]);
463 //------------------------------------------------------------------
464 headset_info = headset;
465 schedule_delayed_work(&headset->h_delayed_work[HEADSET], msecs_to_jiffies(500));
469 platform_set_drvdata(pdev, NULL);
470 input_free_device(headset->input_dev);
472 dev_err(&pdev->dev, "failed to headset probe\n");