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);
84 #if defined(CONFIG_SND_SOC_ES8316)
85 extern int es8316_headset_detect(int jack_insert);
87 #if defined(CONFIG_SND_SOC_CX2072X)
88 extern int cx2072x_jack_report(void);
91 /* headset private data */
93 struct input_dev *input_dev;
94 struct rk_headset_pdata *pdata;
95 unsigned int headset_status:1;
96 unsigned int hook_status:1;
98 struct iio_channel *chan;
99 unsigned int heatset_irq_working;// headset interrupt working will not check hook key
100 int cur_headset_status;
103 unsigned int irq_type[2];
104 struct delayed_work h_delayed_work[2];
105 struct switch_dev sdev;
106 struct mutex mutex_lock[2];
107 unsigned char *keycodes;
108 struct delayed_work hook_work;
109 unsigned int hook_time;//ms
111 static struct headset_priv *headset_info;
114 static irqreturn_t headset_interrupt(int irq, void *dev_id)
116 struct rk_headset_pdata *pdata = headset_info->pdata;
117 static unsigned int old_status = 0;
120 disable_irq_nosync(headset_info->irq[HEADSET]);
121 if(headset_info->heatset_irq_working == BUSY || headset_info->heatset_irq_working == WAIT)
124 DBG("In the headset_interrupt\n");
125 headset_info->heatset_irq_working = BUSY;
129 level = gpio_get_value(pdata->headset_gpio);
132 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->headset_gpio,i);
141 printk("%s:get pin level err!\n",__FUNCTION__);
145 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->headset_gpio,i);
147 old_status = headset_info->headset_status;
148 switch(pdata->headset_insert_type)
150 case HEADSET_IN_HIGH:
152 headset_info->headset_status = HEADSET_IN;
154 headset_info->headset_status = HEADSET_OUT;
158 headset_info->headset_status = HEADSET_IN;
160 headset_info->headset_status = HEADSET_OUT;
163 DBG("---- ERROR: on headset headset_insert_type error -----\n");
166 if(old_status == headset_info->headset_status)
168 DBG("Read Headset IO level old status == now status =%d\n",headset_info->headset_status);
172 DBG("(headset in is %s)headset status is %s\n",
173 pdata->headset_insert_type?"high level":"low level",
174 headset_info->headset_status?"in":"out");
176 #if defined(CONFIG_SND_SOC_ES8316)
177 es8316_headset_detect(headset_info->headset_status);
180 if(headset_info->headset_status == HEADSET_IN)
185 schedule_delayed_work(&headset_info->h_delayed_work[HOOK],msecs_to_jiffies(200));
189 headset_info->isMic= 0;//No microphone
190 headset_info->cur_headset_status = BIT_HEADSET_NO_MIC;
192 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
193 DBG("headset notice android headset status = %d\n",headset_info->cur_headset_status);
196 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
197 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_FALLING);
199 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_RISING);
201 else if(headset_info->headset_status == HEADSET_OUT)
203 headset_info->cur_headset_status = HEADSET_OUT;
204 cancel_delayed_work(&headset_info->hook_work);
205 if(headset_info->isMic)
207 headset_info->hook_status = HOOK_UP;
208 #ifdef CONFIG_SND_SOC_WM8994
209 //rt5625_headset_mic_detect(false);
210 wm8994_headset_mic_detect(false);
212 #if defined (CONFIG_SND_SOC_RT3261) || defined (CONFIG_SND_SOC_RT3224)
213 rt3261_headset_mic_detect(false);
215 #ifdef CONFIG_SND_SOC_RT5631_PHONE
216 rt5631_headset_mic_detect(false);
218 headset_info->isMic = 0;
221 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
222 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_RISING);
224 irq_set_irq_type(headset_info->irq[HEADSET],IRQF_TRIGGER_FALLING);
226 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
227 DBG("headset notice android headset status = %d\n",headset_info->cur_headset_status);
229 // rk_send_wakeup_key();
231 headset_info->heatset_irq_working = IDLE;
232 enable_irq(headset_info->irq[HEADSET]);
236 static int headset_change_irqtype(int type,unsigned int irq_type)
239 free_irq(headset_info->irq[type],NULL);
241 DBG("%s: type is %s irqtype is %s\n",__FUNCTION__, type?"hook":"headset",(irq_type == IRQF_TRIGGER_RISING)?"RISING":"FALLING");
242 // DBG("%s: type is %s irqtype is %s\n",__FUNCTION__, type?"hook":"headset",(irq_type == IRQF_TRIGGER_LOW)?"LOW":"HIGH");
246 ret = request_threaded_irq(headset_info->irq[type],NULL, headset_interrupt, irq_type, "headset_input", NULL);
248 DBG("headset_change_irqtype: request irq failed\n");
257 static void hook_once_work(struct work_struct *work)
261 #ifdef CONFIG_SND_SOC_WM8994
262 wm8994_headset_mic_detect(true);
265 #if defined (CONFIG_SND_SOC_RT3261) || defined (CONFIG_SND_SOC_RT3224)
266 rt3261_headset_mic_detect(true);
269 #ifdef CONFIG_SND_SOC_RT5631_PHONE
270 rt5631_headset_mic_detect(true);
273 ret = iio_read_channel_raw(headset_info->chan, &val);
275 pr_err("read hook_once_work adc channel() error: %d\n", ret);
278 DBG("hook_once_work read adc value: %d\n",val);
280 if(val >= 0 && val < HOOK_LEVEL_LOW)
282 headset_info->isMic= 0;//No microphone
283 #ifdef CONFIG_SND_SOC_WM8994
284 wm8994_headset_mic_detect(false);
287 #if defined (CONFIG_SND_SOC_RT3261) || defined (CONFIG_SND_SOC_RT3224)
288 rt3261_headset_mic_detect(false);
291 #ifdef CONFIG_SND_SOC_RT5631_PHONE
292 rt5631_headset_mic_detect(false);
295 else if(val >= HOOK_LEVEL_HIGH)
297 headset_info->isMic = 1;//have mic
298 schedule_delayed_work(&headset_info->hook_work,msecs_to_jiffies(100));
301 headset_info->cur_headset_status = headset_info->isMic ? BIT_HEADSET:BIT_HEADSET_NO_MIC;
303 #if defined(CONFIG_SND_SOC_CX2072X)
304 if (cx2072x_jack_report() != -1)
305 headset_info->cur_headset_status =
306 (cx2072x_jack_report() == 3) ?
307 BIT_HEADSET : BIT_HEADSET_NO_MIC;
310 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
311 DBG("%s notice android headset status = %d\n",__func__,headset_info->cur_headset_status);
315 static void headsetobserve_work(struct work_struct *work)
317 struct rk_headset_pdata *pdata = headset_info->pdata;
319 DBG("In the headsetobserve_work headset_status is %s\n",headset_info->headset_status?"in":"out");
321 if(headset_info->heatset_irq_working == WAIT && headset_info->headset_status == HEADSET_IN)
323 printk("wait for codec\n");
324 headset_info->heatset_irq_working = IDLE;
325 headset_info->headset_status = HEADSET_OUT;
327 free_irq(headset_info->irq[HEADSET],NULL);
329 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
330 headset_info->irq_type[HEADSET] = IRQF_TRIGGER_LOW|IRQF_ONESHOT;
332 headset_info->irq_type[HEADSET] = IRQF_TRIGGER_HIGH|IRQF_ONESHOT;
333 if(request_threaded_irq(headset_info->irq[HEADSET], NULL,headset_interrupt, headset_info->irq_type[HEADSET]|IRQF_NO_SUSPEND, "headset_input", NULL) < 0)
334 printk("headset request_threaded_irq error\n");
338 if(pdata->headset_insert_type == HEADSET_IN_HIGH && headset_info->headset_status == HEADSET_IN)
339 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);
340 else if(pdata->headset_insert_type == HEADSET_IN_LOW && headset_info->headset_status == HEADSET_IN)
341 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);
343 if(pdata->headset_insert_type == HEADSET_IN_HIGH && headset_info->headset_status == HEADSET_OUT)
344 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);
345 else if(pdata->headset_insert_type == HEADSET_IN_LOW && headset_info->headset_status == HEADSET_OUT)
346 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);
350 static void hook_work_callback(struct work_struct *work)
353 struct headset_priv *headset = headset_info;
354 struct rk_headset_pdata *pdata = headset->pdata;
355 static unsigned int old_status = HOOK_UP;
358 ret = iio_read_channel_raw(headset->chan, &val);
360 pr_err("read hook adc channel() error: %d\n", ret);
364 DBG("hook_work_callback read adc value=%d\n",val);
366 if(headset->headset_status == HEADSET_OUT
367 || headset->heatset_irq_working == BUSY
368 || headset->heatset_irq_working == WAIT
369 || pdata->headset_insert_type?gpio_get_value(pdata->headset_gpio) == 0:gpio_get_value(pdata->headset_gpio) > 0)
371 DBG("Headset is out or waiting for headset is in or out,after same time check HOOK key\n");
375 old_status = headset->hook_status;
376 if(val < HOOK_LEVEL_LOW && val >= 0)
377 headset->hook_status = HOOK_DOWN;
378 else if(val > HOOK_LEVEL_HIGH && val < HOOK_DEFAULT_VAL)
379 headset->hook_status = HOOK_UP;
381 DBG("HOOK status is %s , adc value = %d hook_time = %d\n",headset->hook_status?"down":"up",val,headset->hook_time);
383 if(old_status == headset->hook_status)
385 DBG("Hook adc read old_status == headset->hook_status=%d hook_time = %d\n",headset->hook_status,headset->hook_time);
389 if(headset->headset_status == HEADSET_OUT
390 || headset->heatset_irq_working == BUSY
391 || headset->heatset_irq_working == WAIT
392 || (pdata->headset_insert_type?gpio_get_value(pdata->headset_gpio) == 0:gpio_get_value(pdata->headset_gpio) > 0))
394 printk("headset is out,HOOK status must discard\n");
399 input_report_key(headset->input_dev,HOOK_KEY_CODE,headset->hook_status);
400 input_sync(headset->input_dev);
403 schedule_delayed_work(&headset_info->hook_work,msecs_to_jiffies(100));
407 static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
409 return sprintf(buf, "Headset\n");
413 static int rk_hskey_open(struct input_dev *dev)
418 static void rk_hskey_close(struct input_dev *dev)
423 int rk_headset_adc_probe(struct platform_device *pdev,struct rk_headset_pdata *pdata)
426 struct headset_priv *headset;
428 headset =devm_kzalloc(&pdev->dev,sizeof(struct headset_priv), GFP_KERNEL);
429 if (headset == NULL) {
430 dev_err(&pdev->dev, "failed to allocate driver data\n");
433 headset_info = headset;
434 headset->pdata = pdata;
435 headset->headset_status = HEADSET_OUT;
436 headset->heatset_irq_working = IDLE;
437 headset->hook_status = HOOK_UP;
438 headset->hook_time = HOOK_ADC_SAMPLE_TIME;
439 headset->cur_headset_status = 0;
440 headset->sdev.name = "h2w";
441 headset->sdev.print_name = h2w_print_name;
442 ret = switch_dev_register(&headset->sdev);
446 // mutex_init(&headset->mutex_lock[HEADSET]);
447 // mutex_init(&headset->mutex_lock[HOOK]);
448 INIT_DELAYED_WORK(&headset->h_delayed_work[HEADSET], headsetobserve_work);
449 INIT_DELAYED_WORK(&headset->h_delayed_work[HOOK], hook_once_work);
452 //------------------------------------------------------------------
453 // Create and register the input driver.
454 headset->input_dev = input_allocate_device();
455 if (!headset->input_dev) {
456 dev_err(&pdev->dev, "failed to allocate input device\n");
460 headset->input_dev->name = pdev->name;
461 headset->input_dev->open = rk_hskey_open;
462 headset->input_dev->close = rk_hskey_close;
463 headset->input_dev->dev.parent = &pdev->dev;
464 //input_dev->phys = KEY_PHYS_NAME;
465 headset->input_dev->id.vendor = 0x0001;
466 headset->input_dev->id.product = 0x0001;
467 headset->input_dev->id.version = 0x0100;
468 // Register the input device
469 ret = input_register_device(headset->input_dev);
471 dev_err(&pdev->dev, "failed to register input device\n");
472 goto failed_free_dev;
475 input_set_capability(headset->input_dev, EV_KEY, HOOK_KEY_CODE);
477 if (pdata->headset_gpio) {
478 headset->irq[HEADSET] = gpio_to_irq(pdata->headset_gpio);
480 if(pdata->headset_insert_type == HEADSET_IN_HIGH)
481 headset->irq_type[HEADSET] = IRQF_TRIGGER_HIGH|IRQF_ONESHOT;
483 headset->irq_type[HEADSET] = IRQF_TRIGGER_LOW|IRQF_ONESHOT;
484 ret = request_threaded_irq(headset->irq[HEADSET], NULL,headset_interrupt, headset->irq_type[HEADSET]|IRQF_NO_SUSPEND, "headset_input", NULL);
486 goto failed_free_dev;
487 if (pdata->headset_wakeup)
488 enable_irq_wake(headset->irq[HEADSET]);
490 dev_err(&pdev->dev, "failed init headset,please full hook_io_init function in board\n");
491 goto failed_free_dev;
494 if(pdata->chan != NULL)
496 headset->chan = pdata->chan;
497 INIT_DELAYED_WORK(&headset->hook_work, hook_work_callback);
503 platform_set_drvdata(pdev, NULL);
504 input_free_device(headset->input_dev);
506 dev_err(&pdev->dev, "failed to headset probe\n");
510 int rk_headset_adc_suspend(struct platform_device *pdev, pm_message_t state)
512 DBG("%s----%d\n",__FUNCTION__,__LINE__);
513 // disable_irq(headset_info->irq[HEADSET]);
514 // del_timer(&headset_info->hook_timer);
518 int rk_headset_adc_resume(struct platform_device *pdev)
520 DBG("%s----%d\n",__FUNCTION__,__LINE__);
521 // enable_irq(headset_info->irq[HEADSET]);
522 // if(headset_info->isMic)
523 // mod_timer(&headset_info->hook_timer, jiffies + msecs_to_jiffies(1500));