2 * Copyright (C) 2009 Rockchip Corporation.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/module.h>
15 #include <linux/device.h>
17 #include <linux/interrupt.h>
18 #include <linux/workqueue.h>
19 #include <linux/irq.h>
20 #include <linux/delay.h>
21 #include <linux/types.h>
22 #include <linux/input.h>
23 #include <linux/platform_device.h>
24 #include <linux/mutex.h>
25 #include <linux/errno.h>
26 #include <linux/err.h>
27 #include <linux/hrtimer.h>
28 #include <linux/switch.h>
29 #include <linux/input.h>
30 #include <linux/debugfs.h>
31 #include <linux/wakelock.h>
33 #include <linux/i2c.h>
34 #include <linux/spi/spi.h>
35 #include <linux/iio/consumer.h>
36 #include <linux/adc.h>
37 #include <linux/wakelock.h>
40 #include <asm/atomic.h>
41 #include <asm/mach-types.h>
43 #include "rk_headset.h"
47 #define DBG(x...) printk(x)
49 #define DBG(x...) do { } while (0)
52 #define HOOK_ADC_SAMPLE_TIME 100
53 #define HOOK_LEVEL_HIGH 410 //1V*1024/2.5
54 #define HOOK_LEVEL_LOW 204 //0.5V*1024/2.5
55 #define HOOK_DEFAULT_VAL 1024
57 #define BIT_HEADSET (1 << 0)
58 #define BIT_HEADSET_NO_MIC (1 << 1)
70 #define HEADSET_TIMER 1
77 #ifdef CONFIG_SND_SOC_WM8994
78 extern int wm8994_headset_mic_detect(bool headset_status);
80 #ifdef CONFIG_SND_SOC_RT5631_PHONE
81 extern int rt5631_headset_mic_detect(bool headset_status);
83 #if defined (CONFIG_SND_SOC_RT3261) || defined (CONFIG_SND_SOC_RT3224)
84 extern int rt3261_headset_mic_detect(int jack_insert);
87 /* headset private data */
89 struct input_dev *input_dev;
90 struct rk_headset_pdata *pdata;
91 unsigned int headset_status:1;
92 unsigned int hook_status:1;
94 struct iio_channel *chan;
95 unsigned int heatset_irq_working;// headset interrupt working will not check hook key
96 int cur_headset_status;
99 unsigned int irq_type[2];
100 struct delayed_work h_delayed_work[2];
101 struct switch_dev sdev;
102 struct mutex mutex_lock[2];
103 unsigned char *keycodes;
104 struct delayed_work hook_work;
105 unsigned int hook_time;//ms
107 static struct headset_priv *headset_info;
110 static irqreturn_t headset_interrupt(int irq, void *dev_id)
112 struct rk_headset_pdata *pdata = headset_info->pdata;
113 static unsigned int old_status = 0;
116 disable_irq_nosync(headset_info->irq[HEADSET]);
117 if(headset_info->heatset_irq_working == BUSY || headset_info->heatset_irq_working == WAIT)
120 DBG("In the headset_interrupt\n");
121 headset_info->heatset_irq_working = BUSY;
125 level = gpio_get_value(pdata->headset_gpio);
128 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->headset_gpio,i);
137 printk("%s:get pin level err!\n",__FUNCTION__);
141 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->headset_gpio,i);
143 old_status = headset_info->headset_status;
144 switch(pdata->headset_insert_type)
146 case HEADSET_IN_HIGH:
148 headset_info->headset_status = HEADSET_IN;
150 headset_info->headset_status = HEADSET_OUT;
154 headset_info->headset_status = HEADSET_IN;
156 headset_info->headset_status = HEADSET_OUT;
159 DBG("---- ERROR: on headset headset_insert_type error -----\n");
162 if(old_status == headset_info->headset_status)
164 DBG("Read Headset IO level old status == now status =%d\n",headset_info->headset_status);
168 DBG("(headset in is %s)headset status is %s\n",
169 pdata->headset_insert_type?"high level":"low level",
170 headset_info->headset_status?"in":"out");
171 if(headset_info->headset_status == HEADSET_IN)
176 schedule_delayed_work(&headset_info->h_delayed_work[HOOK],msecs_to_jiffies(200));
180 headset_info->isMic= 0;//No microphone
181 headset_info->cur_headset_status = BIT_HEADSET_NO_MIC;
183 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
184 DBG("headset notice android headset status = %d\n",headset_info->cur_headset_status);
187 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
188 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_FALLING);
190 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_RISING);
192 else if(headset_info->headset_status == HEADSET_OUT)
194 headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
195 cancel_delayed_work(&headset_info->hook_work);
196 if(headset_info->isMic)
198 headset_info->hook_status = HOOK_UP;
199 #ifdef CONFIG_SND_SOC_WM8994
200 //rt5625_headset_mic_detect(false);
201 wm8994_headset_mic_detect(false);
203 #if defined (CONFIG_SND_SOC_RT3261) || defined (CONFIG_SND_SOC_RT3224)
204 rt3261_headset_mic_detect(false);
206 #ifdef CONFIG_SND_SOC_RT5631_PHONE
207 rt5631_headset_mic_detect(false);
211 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
212 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_RISING);
214 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_FALLING);
216 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
217 DBG("headset notice android headset status = %d\n",headset_info->cur_headset_status);
219 // rk_send_wakeup_key();
221 headset_info->heatset_irq_working = IDLE;
222 enable_irq(headset_info->irq[HEADSET]);
226 static int headset_change_irqtype(int type,unsigned int irq_type)
229 free_irq(headset_info->irq[type],NULL);
231 DBG("%s: type is %s irqtype is %s\n",__FUNCTION__, type?"hook":"headset",(irq_type == IRQF_TRIGGER_RISING)?"RISING":"FALLING");
232 // DBG("%s: type is %s irqtype is %s\n",__FUNCTION__, type?"hook":"headset",(irq_type == IRQF_TRIGGER_LOW)?"LOW":"HIGH");
236 ret = request_threaded_irq(headset_info->irq[type],NULL, headset_interrupt, irq_type, "headset_input", NULL);
238 DBG("headset_change_irqtype: request irq failed\n");
247 static void hook_once_work(struct work_struct *work)
251 #ifdef CONFIG_SND_SOC_WM8994
252 wm8994_headset_mic_detect(true);
255 #if defined (CONFIG_SND_SOC_RT3261) || defined (CONFIG_SND_SOC_RT3224)
256 rt3261_headset_mic_detect(true);
259 #ifdef CONFIG_SND_SOC_RT5631_PHONE
260 rt5631_headset_mic_detect(true);
263 ret = iio_read_channel_raw(headset_info->chan, &val);
265 pr_err("read hook_once_work adc channel() error: %d\n", ret);
268 DBG("hook_once_work read adc value: %d\n",val);
270 if(val >= 0 && val < HOOK_LEVEL_LOW)
272 headset_info->isMic= 0;//No microphone
273 #ifdef CONFIG_SND_SOC_WM8994
274 wm8994_headset_mic_detect(false);
277 #if defined (CONFIG_SND_SOC_RT3261) || defined (CONFIG_SND_SOC_RT3224)
278 rt3261_headset_mic_detect(false);
281 #ifdef CONFIG_SND_SOC_RT5631_PHONE
282 rt5631_headset_mic_detect(false);
285 else if(val >= HOOK_LEVEL_HIGH)
287 headset_info->isMic = 1;//have mic
288 schedule_delayed_work(&headset_info->hook_work,msecs_to_jiffies(100));
291 headset_info->cur_headset_status = headset_info->isMic ? BIT_HEADSET:BIT_HEADSET_NO_MIC;
293 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
294 DBG("%s notice android headset status = %d\n",__func__,headset_info->cur_headset_status);
298 static void headsetobserve_work(struct work_struct *work)
300 struct rk_headset_pdata *pdata = headset_info->pdata;
302 DBG("In the headsetobserve_work headset_status is %s\n",headset_info->headset_status?"in":"out");
304 if(headset_info->heatset_irq_working == WAIT && headset_info->headset_status == HEADSET_IN)
306 printk("wait for codec\n");
307 headset_info->heatset_irq_working = IDLE;
308 headset_info->headset_status = HEADSET_OUT;
310 free_irq(headset_info->irq[HEADSET],NULL);
312 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
313 headset_info->irq_type[HEADSET] = IRQF_TRIGGER_LOW|IRQF_ONESHOT;
315 headset_info->irq_type[HEADSET] = IRQF_TRIGGER_HIGH|IRQF_ONESHOT;
316 if(request_threaded_irq(headset_info->irq[HEADSET], NULL,headset_interrupt, headset_info->irq_type[HEADSET]|IRQF_NO_SUSPEND, "headset_input", NULL) < 0)
317 printk("headset request_threaded_irq error\n");
321 if(pdata->headset_insert_type == HEADSET_IN_HIGH && headset_info->headset_status == HEADSET_IN)
322 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);
323 else if(pdata->headset_insert_type == HEADSET_IN_LOW && headset_info->headset_status == HEADSET_IN)
324 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);
326 if(pdata->headset_insert_type == HEADSET_IN_HIGH && headset_info->headset_status == HEADSET_OUT)
327 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);
328 else if(pdata->headset_insert_type == HEADSET_IN_LOW && headset_info->headset_status == HEADSET_OUT)
329 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);
333 static void hook_work_callback(struct work_struct *work)
336 struct headset_priv *headset = headset_info;
337 struct rk_headset_pdata *pdata = headset->pdata;
338 static unsigned int old_status = HOOK_UP;
341 ret = iio_read_channel_raw(headset->chan, &val);
343 pr_err("read hook adc channel() error: %d\n", ret);
347 DBG("hook_work_callback read adc value=%d\n",val);
349 if(headset->headset_status == HEADSET_OUT
350 || headset->heatset_irq_working == BUSY
351 || headset->heatset_irq_working == WAIT
352 || pdata->headset_insert_type?gpio_get_value(pdata->headset_gpio) == 0:gpio_get_value(pdata->headset_gpio) > 0)
354 DBG("Headset is out or waiting for headset is in or out,after same time check HOOK key\n");
358 old_status = headset->hook_status;
359 if(val < HOOK_LEVEL_LOW && val >= 0)
360 headset->hook_status = HOOK_DOWN;
361 else if(val > HOOK_LEVEL_HIGH && val < HOOK_DEFAULT_VAL)
362 headset->hook_status = HOOK_UP;
364 DBG("HOOK status is %s , adc value = %d hook_time = %d\n",headset->hook_status?"down":"up",val,headset->hook_time);
366 if(old_status == headset->hook_status)
368 DBG("Hook adc read old_status == headset->hook_status=%d hook_time = %d\n",headset->hook_status,headset->hook_time);
372 if(headset->headset_status == HEADSET_OUT
373 || headset->heatset_irq_working == BUSY
374 || headset->heatset_irq_working == WAIT
375 || (pdata->headset_insert_type?gpio_get_value(pdata->headset_gpio) == 0:gpio_get_value(pdata->headset_gpio) > 0))
377 printk("headset is out,HOOK status must discard\n");
382 input_report_key(headset->input_dev,HOOK_KEY_CODE,headset->hook_status);
383 input_sync(headset->input_dev);
386 schedule_delayed_work(&headset_info->hook_work,msecs_to_jiffies(100));
390 static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
392 return sprintf(buf, "Headset\n");
396 static int rk_hskey_open(struct input_dev *dev)
401 static void rk_hskey_close(struct input_dev *dev)
406 int rk_headset_adc_probe(struct platform_device *pdev,struct rk_headset_pdata *pdata)
409 struct headset_priv *headset;
411 headset =devm_kzalloc(&pdev->dev,sizeof(struct headset_priv), GFP_KERNEL);
412 if (headset == NULL) {
413 dev_err(&pdev->dev, "failed to allocate driver data\n");
416 headset_info = headset;
417 headset->pdata = pdata;
418 headset->headset_status = HEADSET_OUT;
419 headset->heatset_irq_working = IDLE;
420 headset->hook_status = HOOK_UP;
421 headset->hook_time = HOOK_ADC_SAMPLE_TIME;
422 headset->cur_headset_status = 0;
423 headset->sdev.name = "h2w";
424 headset->sdev.print_name = h2w_print_name;
425 ret = switch_dev_register(&headset->sdev);
429 // mutex_init(&headset->mutex_lock[HEADSET]);
430 // mutex_init(&headset->mutex_lock[HOOK]);
431 INIT_DELAYED_WORK(&headset->h_delayed_work[HEADSET], headsetobserve_work);
432 INIT_DELAYED_WORK(&headset->h_delayed_work[HOOK], hook_once_work);
435 //------------------------------------------------------------------
436 // Create and register the input driver.
437 headset->input_dev = input_allocate_device();
438 if (!headset->input_dev) {
439 dev_err(&pdev->dev, "failed to allocate input device\n");
443 headset->input_dev->name = pdev->name;
444 headset->input_dev->open = rk_hskey_open;
445 headset->input_dev->close = rk_hskey_close;
446 headset->input_dev->dev.parent = &pdev->dev;
447 //input_dev->phys = KEY_PHYS_NAME;
448 headset->input_dev->id.vendor = 0x0001;
449 headset->input_dev->id.product = 0x0001;
450 headset->input_dev->id.version = 0x0100;
451 // Register the input device
452 ret = input_register_device(headset->input_dev);
454 dev_err(&pdev->dev, "failed to register input device\n");
455 goto failed_free_dev;
458 input_set_capability(headset->input_dev, EV_KEY, HOOK_KEY_CODE);
459 //------------------------------------------------------------------
460 if (pdata->headset_gpio) {
461 if(!pdata->headset_gpio){
462 dev_err(&pdev->dev,"failed init headset,please full hook_io_init function in board\n");
463 goto failed_free_dev;
466 headset->irq[HEADSET] = gpio_to_irq(pdata->headset_gpio);
468 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
469 headset->irq_type[HEADSET] = IRQF_TRIGGER_HIGH|IRQF_ONESHOT;
471 headset->irq_type[HEADSET] = IRQF_TRIGGER_LOW|IRQF_ONESHOT;
472 ret = request_threaded_irq(headset->irq[HEADSET], NULL,headset_interrupt, headset->irq_type[HEADSET]|IRQF_NO_SUSPEND, "headset_input", NULL);
474 goto failed_free_dev;
475 enable_irq_wake(headset->irq[HEADSET]);
478 goto failed_free_dev;
479 //------------------------------------------------------------------
480 if(pdata->chan != NULL)
482 headset->chan = pdata->chan;
483 INIT_DELAYED_WORK(&headset->hook_work, hook_work_callback);
489 platform_set_drvdata(pdev, NULL);
490 input_free_device(headset->input_dev);
492 dev_err(&pdev->dev, "failed to headset probe\n");
496 int rk_headset_adc_suspend(struct platform_device *pdev, pm_message_t state)
498 DBG("%s----%d\n",__FUNCTION__,__LINE__);
499 // disable_irq(headset_info->irq[HEADSET]);
500 // del_timer(&headset_info->hook_timer);
504 int rk_headset_adc_resume(struct platform_device *pdev)
506 DBG("%s----%d\n",__FUNCTION__,__LINE__);
507 // enable_irq(headset_info->irq[HEADSET]);
508 // if(headset_info->isMic)
509 // mod_timer(&headset_info->hook_timer, jiffies + msecs_to_jiffies(1500));