rk29_phonesdk: add headset hook key device
[firefly-linux-kernel-4.4.55.git] / drivers / headset_observe / rk_headset.c
1 /* arch/arm/mach-rockchip/rk28_headset.c
2  *
3  * Copyright (C) 2009 Rockchip Corporation.
4  *
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.
8  *
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.
13  *
14  */
15
16 #include <linux/module.h>
17 #include <linux/sysdev.h>
18 #include <linux/device.h>
19 #include <linux/fs.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>
35 #include <asm/gpio.h>
36 #include <asm/atomic.h>
37 #include <asm/mach-types.h>
38 #include "rk_headset.h"
39 #include <linux/earlysuspend.h>
40
41 /* Debug */
42 #if 0
43 #define DBG(x...) printk(x)
44 #else
45 #define DBG(x...) do { } while (0)
46 #endif
47
48 #define BIT_HEADSET             (1 << 0)
49 #define BIT_HEADSET_NO_MIC      (1 << 1)
50
51 #define HEADSET 0
52 #define HOOK 1
53
54 /* headset private data */
55 struct headset_priv {
56         struct input_dev *input_dev;
57         struct rk_headset_pdata *pdata;
58         unsigned int headset_status:1;
59         unsigned int hook_status:1;
60         
61         struct switch_dev sdev;
62         int cur_headset_status; 
63         struct mutex mutex_lock[2];     
64         
65         unsigned int irq[2];
66         unsigned int irq_type[2];
67         struct delayed_work h_delayed_work[2];          
68         
69         unsigned char *keycodes;
70 };
71 static struct headset_priv *headset_info;
72
73
74 static irqreturn_t headset_interrupt(int irq, void *dev_id)
75 {
76 //      DBG("---headset_interrupt---\n");       
77         schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(20));
78         return IRQ_HANDLED;
79 }
80
81 static irqreturn_t Hook_interrupt(int irq, void *dev_id)
82 {
83 //      DBG("---Hook_interrupt---\n");  
84         schedule_delayed_work(&headset_info->h_delayed_work[HOOK], msecs_to_jiffies(100));
85         return IRQ_HANDLED;
86 }
87
88 static int headset_change_irqtype(int type,unsigned int irq_type)
89 {
90         int ret = 0;
91 //      DBG("--------%s----------\n",__FUNCTION__);
92         free_irq(headset_info->irq[type],NULL);
93         
94         switch(type)
95         {
96         case HOOK:
97                 ret = request_irq(headset_info->irq[type], Hook_interrupt, irq_type, NULL, NULL);
98                 break;
99         case HEADSET:
100                 ret = request_irq(headset_info->irq[type], headset_interrupt, irq_type, NULL, NULL);
101                 break;
102         default:
103                 ret = -1;
104                 break;
105         }
106
107         if (ret<0) 
108         {
109                 DBG("headset_change_irqtype: request irq failed\n");
110         return ret;
111         }
112         return ret;
113 }
114
115 static void headsetobserve_work(struct work_struct *work)
116 {
117         int i,level = 0;
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]);
122
123         for(i=0; i<3; i++)
124         {
125                 level = gpio_get_value(pdata->Headset_gpio);
126                 if(level < 0)
127                 {
128                         printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Headset_gpio,i);
129                         msleep(1);
130                         continue;
131                 }
132                 else
133                 break;
134         }
135         if(level < 0)
136         {
137                 printk("%s:get pin level  err!\n",__FUNCTION__);
138                 return;
139         }
140
141         old_status = headset_info->headset_status;
142         switch(pdata->headset_in_type)
143         {
144         case HEADSET_IN_HIGH:
145                 if(level > 0)
146                         headset_info->headset_status = 1;
147                 else if(level == 0)
148                         headset_info->headset_status = 0;       
149                 break;
150         case HEADSET_IN_LOW:
151                 if(level == 0)
152                         headset_info->headset_status = 1;
153                 else if(level > 0)
154                         headset_info->headset_status = 0;               
155                 break;                  
156         default:
157                 DBG("---- ERROR: on headset headset_in_type error -----\n");
158                 break;                  
159         }
160         if(old_status == headset_info->headset_status)
161         {
162                 printk("old_status == headset_info->headset_status\n");
163                 mutex_unlock(&headset_info->mutex_lock[HEADSET]);       
164                 return;
165         }
166
167         switch(pdata->headset_in_type)
168         {
169                 case HEADSET_IN_HIGH:
170                         if(level > 0)
171                         {//in--High level
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);//
177                         }
178                         else if(level == 0)
179                         {//out--Low level
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);//
185                         }
186                         break;
187                 case HEADSET_IN_LOW:
188                         if(level == 0)
189                         {//in--High level
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]);
195                         }
196                         else if(level > 0)
197                         {//out--High level
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]);
203                         }
204                         break;                  
205                 default:
206                         DBG("---- ERROR: on headset headset_in_type error -----\n");
207                         break;                  
208         }
209         
210                                         
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);
213
214         mutex_unlock(&headset_info->mutex_lock[HEADSET]);       
215 }
216
217 static void Hook_work(struct work_struct *work)
218 {
219         int i,level = 0;
220         struct rk_headset_pdata *pdata = headset_info->pdata;
221         static unsigned int old_status = 0;
222         
223         DBG("---Hook_work---\n");
224         mutex_lock(&headset_info->mutex_lock[HOOK]);
225         if(headset_info->headset_status == 0)
226         {
227                 printk("Headset is out\n");
228                 mutex_unlock(&headset_info->mutex_lock[HOOK]);
229                 return;
230         }       
231         
232         for(i=0; i<3; i++)
233         {
234                 level = gpio_get_value(pdata->Hook_gpio);
235                 if(level < 0)
236                 {
237                         printk("%s:get pin level again,pin=%d,i=%d\n",__FUNCTION__,pdata->Hook_gpio,i);
238                         msleep(1);
239                         continue;
240                 }
241                 else
242                 break;
243         }
244         if(level < 0)
245         {
246                 printk("%s:get pin level  err!\n",__FUNCTION__);
247                 return;
248         }
249         
250         old_status = headset_info->hook_status;
251         if(level == 0)
252                 headset_info->hook_status = 1;
253         else if(level > 0)      
254                 headset_info->hook_status = 0;
255         if(old_status == headset_info->hook_status)
256         {
257                 printk("old_status == headset_info->hook_status\n");
258                 mutex_unlock(&headset_info->mutex_lock[HOOK]);
259                 return;
260         }       
261         
262         if(level == 0)
263         {
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);    
268         }
269         else if(level > 0)
270         {
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);    
275         }
276
277         mutex_unlock(&headset_info->mutex_lock[HOOK]);
278 }
279
280 static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
281 {
282         return sprintf(buf, "Headset\n");
283 }
284
285 #ifdef CONFIG_HAS_EARLYSUSPEND
286 static void headset_early_resume(struct early_suspend *h)
287 {
288         schedule_delayed_work(&headset_info->h_delayed_work[HEADSET], msecs_to_jiffies(10));
289         //DBG(">>>>>headset_early_resume\n");
290 }
291
292 static struct early_suspend hs_early_suspend;
293 #endif
294
295 static int rk_Hskey_open(struct input_dev *dev)
296 {
297         //struct rk28_adckey *adckey = input_get_drvdata(dev);
298 //      DBG("===========rk_Hskey_open===========\n");
299         return 0;
300 }
301
302 static void rk_Hskey_close(struct input_dev *dev)
303 {
304 //      DBG("===========rk_Hskey_close===========\n");
305 //      struct rk28_adckey *adckey = input_get_drvdata(dev);
306
307 }
308
309 static int rockchip_headsetobserve_probe(struct platform_device *pdev)
310 {
311         int ret;
312         struct headset_priv *headset;
313         struct rk_headset_pdata *pdata;
314         
315         headset = kzalloc(sizeof(struct headset_priv), GFP_KERNEL);
316         if (headset == NULL) {
317                 dev_err(&pdev->dev, "failed to allocate driver data\n");
318                 return -ENOMEM;
319         }       
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);
328         if (ret < 0)
329                 goto failed_free;
330         
331         mutex_init(&headset->mutex_lock[HEADSET]);
332         mutex_init(&headset->mutex_lock[HOOK]);
333         
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);
338         if (ret) 
339                 goto failed_free;
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);
343
344         if(pdata->headset_in_type == HEADSET_IN_HIGH)
345                 headset->irq_type[HEADSET] = IRQF_TRIGGER_RISING;
346         else
347                 headset->irq_type[HEADSET] = IRQF_TRIGGER_FALLING;
348         ret = request_irq(headset->irq[HEADSET], headset_interrupt, headset->irq_type[HEADSET], NULL, NULL);
349         if (ret) 
350                 goto failed_free;
351 //------------------------------------------------------------------
352         ret = gpio_request(pdata->Hook_gpio , NULL);
353         if (ret) 
354                 goto failed_free;
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;
359         
360         ret = request_irq(headset->irq[HOOK], Hook_interrupt, headset->irq_type[HOOK] , NULL, NULL);
361         if (ret) 
362                 goto failed_free;
363         disable_irq(headset->irq[HOOK]);
364 //------------------------------------------------------------------    
365         
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");
370                 ret = -ENOMEM;
371                 goto failed_free;
372         }       
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);
383         if (ret) {
384                 dev_err(&pdev->dev, "failed to register input device\n");
385                 goto failed_free_dev;
386         }
387
388
389 //      headset->input_dev->keycode = headset->keycodes;
390 //      headset->input_dev->keycodesize = sizeof(unsigned char);
391 //      headset->input_dev->keycodemax = 2;
392         
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);
398
399 //      headset->input_dev->evbit[0] = BIT_MASK(EV_KEY);
400         
401         headset_info = headset;
402         schedule_delayed_work(&headset->h_delayed_work[HEADSET], msecs_to_jiffies(500));        
403
404
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);
410 #endif
411
412         return 0;       
413         
414 failed_free_dev:
415         platform_set_drvdata(pdev, NULL);
416         input_free_device(headset->input_dev);
417 failed_free:
418         kfree(headset); 
419         return ret;
420 }
421
422 static struct platform_driver rockchip_headsetobserve_driver = {
423         .probe  = rockchip_headsetobserve_probe,
424         .driver = {
425                 .name   = "rk_headsetdet",
426                 .owner  = THIS_MODULE,
427         },
428 };
429
430 static int __init rockchip_headsetobserve_init(void)
431 {
432         platform_driver_register(&rockchip_headsetobserve_driver);
433         return 0;
434 }
435 module_init(rockchip_headsetobserve_init);
436 MODULE_DESCRIPTION("Rockchip Headset Driver");
437 MODULE_LICENSE("GPL");