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/debugfs.h>
30 #include <linux/wakelock.h>
32 #include <linux/i2c.h>
33 #include <linux/spi/spi.h>
34 #include <linux/iio/consumer.h>
35 #include <linux/adc.h>
36 #include <linux/wakelock.h>
37 #include <linux/gpio.h>
39 #include <asm/atomic.h>
41 #include "rk_headset.h"
45 #define DBG(x...) printk(x)
47 #define DBG(x...) do { } while (0)
50 #define HOOK_ADC_SAMPLE_TIME 100
51 #define HOOK_LEVEL_HIGH 410 //1V*1024/2.5
52 #define HOOK_LEVEL_LOW 204 //0.5V*1024/2.5
53 #define HOOK_DEFAULT_VAL 1024
55 #define BIT_HEADSET (1 << 0)
56 #define BIT_HEADSET_NO_MIC (1 << 1)
68 #define HEADSET_TIMER 1
75 #ifdef CONFIG_SND_SOC_WM8994
76 extern int wm8994_headset_mic_detect(bool headset_status);
78 #ifdef CONFIG_SND_SOC_RT5631_PHONE
79 extern int rt5631_headset_mic_detect(bool headset_status);
81 #if defined (CONFIG_SND_SOC_RT3261) || defined (CONFIG_SND_SOC_RT3224)
82 extern int rt3261_headset_mic_detect(int jack_insert);
85 /* headset private data */
87 struct input_dev *input_dev;
88 struct rk_headset_pdata *pdata;
89 unsigned int headset_status:1;
90 unsigned int hook_status:1;
92 struct iio_channel *chan;
93 unsigned int heatset_irq_working;// headset interrupt working will not check hook key
94 int cur_headset_status;
97 unsigned int irq_type[2];
98 struct delayed_work h_delayed_work[2];
99 struct switch_dev sdev;
100 struct mutex mutex_lock[2];
101 unsigned char *keycodes;
102 struct delayed_work hook_work;
103 unsigned int hook_time;//ms
105 static struct headset_priv *headset_info;
108 static irqreturn_t headset_interrupt(int irq, void *dev_id)
110 struct rk_headset_pdata *pdata = headset_info->pdata;
111 static unsigned int old_status = 0;
114 disable_irq_nosync(headset_info->irq[HEADSET]);
115 if(headset_info->heatset_irq_working == BUSY || headset_info->heatset_irq_working == WAIT)
118 DBG("In the headset_interrupt\n");
119 headset_info->heatset_irq_working = BUSY;
123 level = gpio_get_value(pdata->headset_gpio);
126 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->headset_gpio,i);
135 printk("%s:get pin level err!\n",__FUNCTION__);
139 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->headset_gpio,i);
141 old_status = headset_info->headset_status;
142 switch(pdata->headset_insert_type)
144 case HEADSET_IN_HIGH:
146 headset_info->headset_status = HEADSET_IN;
148 headset_info->headset_status = HEADSET_OUT;
152 headset_info->headset_status = HEADSET_IN;
154 headset_info->headset_status = HEADSET_OUT;
157 DBG("---- ERROR: on headset headset_insert_type error -----\n");
160 if(old_status == headset_info->headset_status)
162 DBG("Read Headset IO level old status == now status =%d\n",headset_info->headset_status);
166 DBG("(headset in is %s)headset status is %s\n",
167 pdata->headset_insert_type?"high level":"low level",
168 headset_info->headset_status?"in":"out");
169 if(headset_info->headset_status == HEADSET_IN)
174 schedule_delayed_work(&headset_info->h_delayed_work[HOOK],msecs_to_jiffies(200));
178 headset_info->isMic= 0;//No microphone
179 headset_info->cur_headset_status = BIT_HEADSET_NO_MIC;
181 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
182 DBG("headset notice android headset status = %d\n",headset_info->cur_headset_status);
185 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
186 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_FALLING);
188 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_RISING);
190 else if(headset_info->headset_status == HEADSET_OUT)
192 headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
193 cancel_delayed_work(&headset_info->hook_work);
194 if(headset_info->isMic)
196 headset_info->hook_status = HOOK_UP;
197 #ifdef CONFIG_SND_SOC_WM8994
198 //rt5625_headset_mic_detect(false);
199 wm8994_headset_mic_detect(false);
201 #if defined (CONFIG_SND_SOC_RT3261) || defined (CONFIG_SND_SOC_RT3224)
202 rt3261_headset_mic_detect(false);
204 #ifdef CONFIG_SND_SOC_RT5631_PHONE
205 rt5631_headset_mic_detect(false);
209 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
210 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_RISING);
212 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_FALLING);
214 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
215 DBG("headset notice android headset status = %d\n",headset_info->cur_headset_status);
217 // rk_send_wakeup_key();
219 headset_info->heatset_irq_working = IDLE;
220 enable_irq(headset_info->irq[HEADSET]);
224 static int headset_change_irqtype(int type,unsigned int irq_type)
227 free_irq(headset_info->irq[type],NULL);
229 DBG("%s: type is %s irqtype is %s\n",__FUNCTION__, type?"hook":"headset",(irq_type == IRQF_TRIGGER_RISING)?"RISING":"FALLING");
230 // DBG("%s: type is %s irqtype is %s\n",__FUNCTION__, type?"hook":"headset",(irq_type == IRQF_TRIGGER_LOW)?"LOW":"HIGH");
234 ret = request_threaded_irq(headset_info->irq[type],NULL, headset_interrupt, irq_type, "headset_input", NULL);
236 DBG("headset_change_irqtype: request irq failed\n");
245 static void hook_once_work(struct work_struct *work)
249 #ifdef CONFIG_SND_SOC_WM8994
250 wm8994_headset_mic_detect(true);
253 #if defined (CONFIG_SND_SOC_RT3261) || defined (CONFIG_SND_SOC_RT3224)
254 rt3261_headset_mic_detect(true);
257 #ifdef CONFIG_SND_SOC_RT5631_PHONE
258 rt5631_headset_mic_detect(true);
261 ret = iio_read_channel_raw(headset_info->chan, &val);
263 pr_err("read hook_once_work adc channel() error: %d\n", ret);
266 DBG("hook_once_work read adc value: %d\n",val);
268 if(val >= 0 && val < HOOK_LEVEL_LOW)
270 headset_info->isMic= 0;//No microphone
271 #ifdef CONFIG_SND_SOC_WM8994
272 wm8994_headset_mic_detect(false);
275 #if defined (CONFIG_SND_SOC_RT3261) || defined (CONFIG_SND_SOC_RT3224)
276 rt3261_headset_mic_detect(false);
279 #ifdef CONFIG_SND_SOC_RT5631_PHONE
280 rt5631_headset_mic_detect(false);
283 else if(val >= HOOK_LEVEL_HIGH)
285 headset_info->isMic = 1;//have mic
286 schedule_delayed_work(&headset_info->hook_work,msecs_to_jiffies(100));
289 headset_info->cur_headset_status = headset_info->isMic ? BIT_HEADSET:BIT_HEADSET_NO_MIC;
291 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
292 DBG("%s notice android headset status = %d\n",__func__,headset_info->cur_headset_status);
296 static void headsetobserve_work(struct work_struct *work)
298 struct rk_headset_pdata *pdata = headset_info->pdata;
300 DBG("In the headsetobserve_work headset_status is %s\n",headset_info->headset_status?"in":"out");
302 if(headset_info->heatset_irq_working == WAIT && headset_info->headset_status == HEADSET_IN)
304 printk("wait for codec\n");
305 headset_info->heatset_irq_working = IDLE;
306 headset_info->headset_status = HEADSET_OUT;
308 free_irq(headset_info->irq[HEADSET],NULL);
310 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
311 headset_info->irq_type[HEADSET] = IRQF_TRIGGER_LOW|IRQF_ONESHOT;
313 headset_info->irq_type[HEADSET] = IRQF_TRIGGER_HIGH|IRQF_ONESHOT;
314 if(request_threaded_irq(headset_info->irq[HEADSET], NULL,headset_interrupt, headset_info->irq_type[HEADSET]|IRQF_NO_SUSPEND, "headset_input", NULL) < 0)
315 printk("headset request_threaded_irq error\n");
319 if(pdata->headset_insert_type == HEADSET_IN_HIGH && headset_info->headset_status == HEADSET_IN)
320 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);
321 else if(pdata->headset_insert_type == HEADSET_IN_LOW && headset_info->headset_status == HEADSET_IN)
322 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);
324 if(pdata->headset_insert_type == HEADSET_IN_HIGH && headset_info->headset_status == HEADSET_OUT)
325 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);
326 else if(pdata->headset_insert_type == HEADSET_IN_LOW && headset_info->headset_status == HEADSET_OUT)
327 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);
331 static void hook_work_callback(struct work_struct *work)
334 struct headset_priv *headset = headset_info;
335 struct rk_headset_pdata *pdata = headset->pdata;
336 static unsigned int old_status = HOOK_UP;
339 ret = iio_read_channel_raw(headset->chan, &val);
341 pr_err("read hook adc channel() error: %d\n", ret);
345 DBG("hook_work_callback read adc value=%d\n",val);
347 if(headset->headset_status == HEADSET_OUT
348 || headset->heatset_irq_working == BUSY
349 || headset->heatset_irq_working == WAIT
350 || pdata->headset_insert_type?gpio_get_value(pdata->headset_gpio) == 0:gpio_get_value(pdata->headset_gpio) > 0)
352 DBG("Headset is out or waiting for headset is in or out,after same time check HOOK key\n");
356 old_status = headset->hook_status;
357 if(val < HOOK_LEVEL_LOW && val >= 0)
358 headset->hook_status = HOOK_DOWN;
359 else if(val > HOOK_LEVEL_HIGH && val < HOOK_DEFAULT_VAL)
360 headset->hook_status = HOOK_UP;
362 DBG("HOOK status is %s , adc value = %d hook_time = %d\n",headset->hook_status?"down":"up",val,headset->hook_time);
364 if(old_status == headset->hook_status)
366 DBG("Hook adc read old_status == headset->hook_status=%d hook_time = %d\n",headset->hook_status,headset->hook_time);
370 if(headset->headset_status == HEADSET_OUT
371 || headset->heatset_irq_working == BUSY
372 || headset->heatset_irq_working == WAIT
373 || (pdata->headset_insert_type?gpio_get_value(pdata->headset_gpio) == 0:gpio_get_value(pdata->headset_gpio) > 0))
375 printk("headset is out,HOOK status must discard\n");
380 input_report_key(headset->input_dev,HOOK_KEY_CODE,headset->hook_status);
381 input_sync(headset->input_dev);
384 schedule_delayed_work(&headset_info->hook_work,msecs_to_jiffies(100));
388 static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
390 return sprintf(buf, "Headset\n");
394 static int rk_hskey_open(struct input_dev *dev)
399 static void rk_hskey_close(struct input_dev *dev)
404 int rk_headset_adc_probe(struct platform_device *pdev,struct rk_headset_pdata *pdata)
407 struct headset_priv *headset;
409 headset =devm_kzalloc(&pdev->dev,sizeof(struct headset_priv), GFP_KERNEL);
410 if (headset == NULL) {
411 dev_err(&pdev->dev, "failed to allocate driver data\n");
414 headset_info = headset;
415 headset->pdata = pdata;
416 headset->headset_status = HEADSET_OUT;
417 headset->heatset_irq_working = IDLE;
418 headset->hook_status = HOOK_UP;
419 headset->hook_time = HOOK_ADC_SAMPLE_TIME;
420 headset->cur_headset_status = 0;
421 headset->sdev.name = "h2w";
422 headset->sdev.print_name = h2w_print_name;
423 ret = switch_dev_register(&headset->sdev);
427 // mutex_init(&headset->mutex_lock[HEADSET]);
428 // mutex_init(&headset->mutex_lock[HOOK]);
429 INIT_DELAYED_WORK(&headset->h_delayed_work[HEADSET], headsetobserve_work);
430 INIT_DELAYED_WORK(&headset->h_delayed_work[HOOK], hook_once_work);
433 //------------------------------------------------------------------
434 // Create and register the input driver.
435 headset->input_dev = input_allocate_device();
436 if (!headset->input_dev) {
437 dev_err(&pdev->dev, "failed to allocate input device\n");
441 headset->input_dev->name = pdev->name;
442 headset->input_dev->open = rk_hskey_open;
443 headset->input_dev->close = rk_hskey_close;
444 headset->input_dev->dev.parent = &pdev->dev;
445 //input_dev->phys = KEY_PHYS_NAME;
446 headset->input_dev->id.vendor = 0x0001;
447 headset->input_dev->id.product = 0x0001;
448 headset->input_dev->id.version = 0x0100;
449 // Register the input device
450 ret = input_register_device(headset->input_dev);
452 dev_err(&pdev->dev, "failed to register input device\n");
453 goto failed_free_dev;
456 input_set_capability(headset->input_dev, EV_KEY, HOOK_KEY_CODE);
457 //------------------------------------------------------------------
458 if (pdata->headset_gpio) {
459 if(!pdata->headset_gpio){
460 dev_err(&pdev->dev,"failed init headset,please full hook_io_init function in board\n");
461 goto failed_free_dev;
464 headset->irq[HEADSET] = gpio_to_irq(pdata->headset_gpio);
466 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
467 headset->irq_type[HEADSET] = IRQF_TRIGGER_HIGH|IRQF_ONESHOT;
469 headset->irq_type[HEADSET] = IRQF_TRIGGER_LOW|IRQF_ONESHOT;
470 ret = request_threaded_irq(headset->irq[HEADSET], NULL,headset_interrupt, headset->irq_type[HEADSET]|IRQF_NO_SUSPEND, "headset_input", NULL);
472 goto failed_free_dev;
473 enable_irq_wake(headset->irq[HEADSET]);
476 goto failed_free_dev;
477 //------------------------------------------------------------------
478 if(pdata->chan != NULL)
480 headset->chan = pdata->chan;
481 INIT_DELAYED_WORK(&headset->hook_work, hook_work_callback);
487 platform_set_drvdata(pdev, NULL);
488 input_free_device(headset->input_dev);
490 dev_err(&pdev->dev, "failed to headset probe\n");
494 int rk_headset_adc_suspend(struct platform_device *pdev, pm_message_t state)
496 DBG("%s----%d\n",__FUNCTION__,__LINE__);
497 // disable_irq(headset_info->irq[HEADSET]);
498 // del_timer(&headset_info->hook_timer);
502 int rk_headset_adc_resume(struct platform_device *pdev)
504 DBG("%s----%d\n",__FUNCTION__,__LINE__);
505 // enable_irq(headset_info->irq[HEADSET]);
506 // if(headset_info->isMic)
507 // mod_timer(&headset_info->hook_timer, jiffies + msecs_to_jiffies(1500));