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/sysdev.h>
18 #include <linux/device.h>
20 #include <linux/interrupt.h>
21 #include <linux/workqueue.h>
22 #include <linux/irq.h>
23 #include <linux/delay.h>
24 #include <linux/types.h>
25 #include <linux/input.h>
26 #include <linux/platform_device.h>
27 #include <linux/mutex.h>
28 #include <linux/errno.h>
29 #include <linux/err.h>
30 #include <linux/hrtimer.h>
31 #include <linux/switch.h>
32 #include <linux/input.h>
33 #include <linux/debugfs.h>
34 #include <linux/wakelock.h>
36 #include <asm/atomic.h>
37 #include <asm/mach-types.h>
38 #include "rk_headset.h"
39 #include <linux/earlysuspend.h>
43 #define DBG(x...) printk(x)
45 #define DBG(x...) do { } while (0)
48 #define BIT_HEADSET (1 << 0)
49 #define BIT_HEADSET_NO_MIC (1 << 1)
54 /* headset private data */
56 struct input_dev *input_dev;
57 struct rk_headset_pdata *pdata;
58 unsigned int headset_status:1;
59 unsigned int hook_status:1;
61 struct switch_dev sdev;
62 int cur_headset_status;
63 struct mutex mutex_lock[2];
66 unsigned int irq_type[2];
67 struct delayed_work h_delayed_work[2];
69 unsigned char *keycodes;
71 static struct headset_priv *headset_info;
74 static irqreturn_t headset_interrupt(int irq, void *dev_id)
76 // DBG("---headset_interrupt---\n");
77 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(20));
81 static irqreturn_t Hook_interrupt(int irq, void *dev_id)
83 // DBG("---Hook_interrupt---\n");
84 schedule_delayed_work(&headset_info->h_delayed_work[HOOK], msecs_to_jiffies(100));
88 static int headset_change_irqtype(int type,unsigned int irq_type)
91 // DBG("--------%s----------\n",__FUNCTION__);
92 free_irq(headset_info->irq[type],NULL);
97 ret = request_irq(headset_info->irq[type], Hook_interrupt, irq_type, NULL, NULL);
100 ret = request_irq(headset_info->irq[type], headset_interrupt, irq_type, NULL, NULL);
109 DBG("headset_change_irqtype: request irq failed\n");
115 static void headsetobserve_work(struct work_struct *work)
118 struct rk_headset_pdata *pdata = headset_info->pdata;
119 static unsigned int old_status = 0;
120 // DBG("---headsetobserve_work---\n");
121 mutex_lock(&headset_info->mutex_lock[HEADSET]);
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 old_status = headset_info->headset_status;
142 switch(pdata->headset_in_type)
144 case HEADSET_IN_HIGH:
146 headset_info->headset_status = 1;
148 headset_info->headset_status = 0;
152 headset_info->headset_status = 1;
154 headset_info->headset_status = 0;
157 DBG("---- ERROR: on headset headset_in_type error -----\n");
160 if(old_status == headset_info->headset_status)
162 printk("old_status == headset_info->headset_status\n");
163 mutex_unlock(&headset_info->mutex_lock[HEADSET]);
167 switch(pdata->headset_in_type)
169 case HEADSET_IN_HIGH:
172 DBG("--- HEADSET_IN_HIGH headset in HIGH---\n");
173 enable_irq(headset_info->irq[HOOK]);
174 headset_info->headset_status = 1;
175 headset_info->cur_headset_status = BIT_HEADSET;
176 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);//
180 DBG("---HEADSET_IN_HIGH headset out HIGH---\n");
181 disable_irq(headset_info->irq[HOOK]);
182 headset_info->headset_status = 0;
183 headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
184 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);//
190 DBG("---HEADSET_IN_LOW headset in LOW ---\n");
191 headset_info->headset_status = 1;
192 headset_info->cur_headset_status = BIT_HEADSET;
193 headset_change_irqtype(HEADSET,IRQF_TRIGGER_RISING);//
194 enable_irq(headset_info->irq[HOOK]);
198 DBG("---HEADSET_IN_LOW headset out LOW ---\n");
199 headset_info->headset_status = 0;
200 headset_info->cur_headset_status = ~(BIT_HEADSET|BIT_HEADSET_NO_MIC);
201 headset_change_irqtype(HEADSET,IRQF_TRIGGER_FALLING);//
202 disable_irq(headset_info->irq[HOOK]);
206 DBG("---- ERROR: on headset headset_in_type error -----\n");
211 switch_set_state(&headset_info->sdev, headset_info->cur_headset_status);
212 DBG("Headset_dev.cur_headset_status = %d\n",headset_info->cur_headset_status);
214 mutex_unlock(&headset_info->mutex_lock[HEADSET]);
217 static void Hook_work(struct work_struct *work)
220 struct rk_headset_pdata *pdata = headset_info->pdata;
221 static unsigned int old_status = 0;
223 DBG("---Hook_work---\n");
224 mutex_lock(&headset_info->mutex_lock[HOOK]);
225 if(headset_info->headset_status == 0)
227 printk("Headset is out\n");
228 mutex_unlock(&headset_info->mutex_lock[HOOK]);
234 level = gpio_get_value(pdata->Hook_gpio);
237 printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Hook_gpio,i);
246 printk("%s:get pin level err!\n",__FUNCTION__);
250 old_status = headset_info->hook_status;
252 headset_info->hook_status = 1;
254 headset_info->hook_status = 0;
255 if(old_status == headset_info->hook_status)
257 printk("old_status == headset_info->hook_status\n");
258 mutex_unlock(&headset_info->mutex_lock[HOOK]);
264 DBG("---HOOK Down ---\n");
265 headset_change_irqtype(HOOK,IRQF_TRIGGER_RISING);//
266 input_report_key(headset_info->input_dev,KEY_MEDIA,headset_info->hook_status);
267 input_sync(headset_info->input_dev);
271 DBG("---HOOK Up ---\n");
272 headset_change_irqtype(HOOK,IRQF_TRIGGER_FALLING);//
273 input_report_key(headset_info->input_dev,KEY_MEDIA,headset_info->hook_status);
274 input_sync(headset_info->input_dev);
277 mutex_unlock(&headset_info->mutex_lock[HOOK]);
280 static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
282 return sprintf(buf, "Headset\n");
285 #ifdef CONFIG_HAS_EARLYSUSPEND
286 static void headset_early_resume(struct early_suspend *h)
288 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(10));
289 //DBG(">>>>>headset_early_resume\n");
292 static struct early_suspend hs_early_suspend;
295 static int rk_Hskey_open(struct input_dev *dev)
297 //struct rk28_adckey *adckey = input_get_drvdata(dev);
298 // DBG("===========rk_Hskey_open===========\n");
302 static void rk_Hskey_close(struct input_dev *dev)
304 // DBG("===========rk_Hskey_close===========\n");
305 // struct rk28_adckey *adckey = input_get_drvdata(dev);
309 static int rockchip_headsetobserve_probe(struct platform_device *pdev)
312 struct headset_priv *headset;
313 struct rk_headset_pdata *pdata;
315 headset = kzalloc(sizeof(struct headset_priv), GFP_KERNEL);
316 if (headset == NULL) {
317 dev_err(&pdev->dev, "failed to allocate driver data\n");
320 headset->pdata = pdev->dev.platform_data;
321 pdata = headset->pdata;
322 headset->headset_status = 0;
323 headset->hook_status = 0;
324 headset->cur_headset_status = 0;
325 headset->sdev.name = "h2w";
326 headset->sdev.print_name = h2w_print_name;
327 ret = switch_dev_register(&headset->sdev);
331 mutex_init(&headset->mutex_lock[HEADSET]);
332 mutex_init(&headset->mutex_lock[HOOK]);
334 INIT_DELAYED_WORK(&headset->h_delayed_work[HEADSET], headsetobserve_work);
335 INIT_DELAYED_WORK(&headset->h_delayed_work[HOOK], Hook_work);
336 //------------------------------------------------------------------
337 ret = gpio_request(pdata->Headset_gpio, NULL);
340 gpio_pull_updown(pdata->Headset_gpio, PullDisable);
341 gpio_direction_input(pdata->Headset_gpio);
342 headset->irq[HEADSET] = gpio_to_irq(pdata->Headset_gpio);
344 if(pdata->headset_in_type == HEADSET_IN_HIGH)
345 headset->irq_type[HEADSET] = IRQF_TRIGGER_RISING;
347 headset->irq_type[HEADSET] = IRQF_TRIGGER_FALLING;
348 ret = request_irq(headset->irq[HEADSET], headset_interrupt, headset->irq_type[HEADSET], NULL, NULL);
351 //------------------------------------------------------------------
352 ret = gpio_request(pdata->Hook_gpio , NULL);
355 gpio_pull_updown(pdata->Hook_gpio, PullDisable);
356 gpio_direction_input(pdata->Hook_gpio);
357 headset->irq[HOOK] = gpio_to_irq(pdata->Hook_gpio);
358 headset->irq_type[HOOK] = IRQF_TRIGGER_FALLING;
360 ret = request_irq(headset->irq[HOOK], Hook_interrupt, headset->irq_type[HOOK] , NULL, NULL);
363 disable_irq(headset->irq[HOOK]);
364 //------------------------------------------------------------------
366 // Create and register the input driver.
367 headset->input_dev = input_allocate_device();
368 if (!headset->input_dev) {
369 dev_err(&pdev->dev, "failed to allocate input device\n");
373 headset->input_dev->name = pdev->name;
374 headset->input_dev->open = rk_Hskey_open;
375 headset->input_dev->close = rk_Hskey_close;
376 headset->input_dev->dev.parent = &pdev->dev;
377 //input_dev->phys = KEY_PHYS_NAME;
378 headset->input_dev->id.vendor = 0x0001;
379 headset->input_dev->id.product = 0x0001;
380 headset->input_dev->id.version = 0x0100;
381 // Register the input device
382 ret = input_register_device(headset->input_dev);
384 dev_err(&pdev->dev, "failed to register input device\n");
385 goto failed_free_dev;
389 // headset->input_dev->keycode = headset->keycodes;
390 // headset->input_dev->keycodesize = sizeof(unsigned char);
391 // headset->input_dev->keycodemax = 2;
393 // set_bit(KEY_MEDIA, headset->input_dev->keybit);
394 // clear_bit(0, headset->input_dev->keybit);
395 input_set_capability(headset->input_dev, EV_KEY, KEY_MEDIA);
396 // input_set_capability(headset->input_dev, EV_SW, SW_HEADPHONE_INSERT);
397 // input_set_capability(headset->input_dev, EV_KEY, KEY_END);
399 // headset->input_dev->evbit[0] = BIT_MASK(EV_KEY);
401 headset_info = headset;
402 schedule_delayed_work(&headset->h_delayed_work[HEADSET], msecs_to_jiffies(500));
405 #ifdef CONFIG_HAS_EARLYSUSPEND
406 hs_early_suspend.suspend = NULL;
407 hs_early_suspend.resume = headset_early_resume;
408 hs_early_suspend.level = ~0x0;
409 register_early_suspend(&hs_early_suspend);
415 platform_set_drvdata(pdev, NULL);
416 input_free_device(headset->input_dev);
422 static struct platform_driver rockchip_headsetobserve_driver = {
423 .probe = rockchip_headsetobserve_probe,
425 .name = "rk_headsetdet",
426 .owner = THIS_MODULE,
430 static int __init rockchip_headsetobserve_init(void)
432 platform_driver_register(&rockchip_headsetobserve_driver);
435 module_init(rockchip_headsetobserve_init);
436 MODULE_DESCRIPTION("Rockchip Headset Driver");
437 MODULE_LICENSE("GPL");