Initial commit
[kernel/linux-3.0.git] / drivers / misc / sec_jack.c
1 /*  drivers/misc/sec_jack.c
2  *
3  *  Copyright (C) 2010 Samsung Electronics Co.Ltd
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  */
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/workqueue.h>
18 #include <linux/irq.h>
19 #include <linux/delay.h>
20 #include <linux/types.h>
21 #include <linux/input.h>
22 #include <linux/platform_device.h>
23 #include <linux/errno.h>
24 #include <linux/err.h>
25 #include <linux/switch.h>
26 #include <linux/input.h>
27 #include <linux/timer.h>
28 #include <linux/wakelock.h>
29 #include <linux/slab.h>
30 #include <linux/gpio.h>
31 #include <linux/gpio_event.h>
32 #include <linux/sec_jack.h>
33
34 #include <plat/adc.h>
35
36 #if defined(CONFIG_STMPE811_ADC)
37 #define SEC_JACK_ADC_CH         4
38 #else
39 #define SEC_JACK_ADC_CH         3
40 #endif
41 #define SEC_JACK_SAMPLE_SIZE    5
42
43 #define MAX_ZONE_LIMIT          10
44 /* keep this value if you support double-pressed concept */
45 #if defined(CONFIG_TARGET_LOCALE_KOR)
46 #define SEND_KEY_CHECK_TIME_MS  20              /* 20ms - GB VOC in KOR*/
47 #elif defined(CONFIG_MACH_Q1_BD)
48 /* 27ms, total delay is approximately double more
49    because hrtimer is called twice by gpio input driver,
50    new sec spec total delay is 60ms +/-10ms */
51 #define SEND_KEY_CHECK_TIME_MS  27
52 #else
53 #define SEND_KEY_CHECK_TIME_MS  40              /* 40ms */
54 #endif
55 #define WAKE_LOCK_TIME          (HZ * 5)        /* 5 sec */
56 #define EAR_CHECK_LOOP_CNT      10
57
58 #if defined(CONFIG_MACH_PX) || defined(CONFIG_MACH_P4NOTE) \
59         || defined(CONFIG_MACH_GC1)
60 #define JACK_CLASS_NAME "audio"
61 #define JACK_DEV_NAME "earjack"
62 #else
63 #define JACK_CLASS_NAME "jack"
64 #define JACK_DEV_NAME "jack_selector"
65 #endif
66 #define JACK_RESELECTOR_NAME "jack_reselector"
67
68 static struct class *jack_class;
69 static struct device *jack_dev;
70 static struct device *jack_reselector;
71 static bool recheck_jack;
72
73 struct sec_jack_info {
74         struct s3c_adc_client *padc;
75         struct sec_jack_platform_data *pdata;
76         struct delayed_work jack_detect_work;
77         struct work_struct buttons_work;
78         struct workqueue_struct *queue;
79         struct input_dev *input_dev;
80         struct wake_lock det_wake_lock;
81         struct sec_jack_zone *zone;
82         struct input_handler handler;
83         struct input_handle handle;
84         struct input_device_id ids[2];
85         int det_irq;
86         int dev_id;
87         int pressed;
88         int pressed_code;
89         struct platform_device *send_key_dev;
90         unsigned int cur_jack_type;
91         int det_status;
92 };
93
94 /* with some modifications like moving all the gpio structs inside
95  * the platform data and getting the name for the switch and
96  * gpio_event from the platform data, the driver could support more than
97  * one headset jack, but currently user space is looking only for
98  * one key file and switch for a headset so it'd be overkill and
99  * untestable so we limit to one instantiation for now.
100  */
101 static atomic_t instantiated = ATOMIC_INIT(0);
102
103 /* sysfs name HeadsetObserver.java looks for to track headset state
104  */
105 struct switch_dev switch_jack_detection = {
106         .name = "h2w",
107 };
108
109 /* To support AT+FCESTEST=1 */
110 struct switch_dev switch_sendend = {
111                 .name = "send_end",
112 };
113
114 static struct gpio_event_direct_entry sec_jack_key_map[] = {
115         {
116                 .code   = KEY_UNKNOWN,
117         },
118 };
119
120 static struct gpio_event_input_info sec_jack_key_info = {
121         .info.func = gpio_event_input_func,
122         .info.no_suspend = true,
123         .type = EV_KEY,
124         .debounce_time.tv64 = SEND_KEY_CHECK_TIME_MS * NSEC_PER_MSEC,
125         .keymap = sec_jack_key_map,
126         .keymap_size = ARRAY_SIZE(sec_jack_key_map)
127 };
128
129 static struct gpio_event_info *sec_jack_input_info[] = {
130         &sec_jack_key_info.info,
131 };
132
133 static struct gpio_event_platform_data sec_jack_input_data = {
134         .name = "sec_jack",
135         .info = sec_jack_input_info,
136         .info_count = ARRAY_SIZE(sec_jack_input_info),
137 };
138
139 static int sec_jack_get_adc_data(struct s3c_adc_client *padc)
140 {
141         int adc_data;
142         int adc_max = 0;
143         int adc_min = 0xFFFF;
144         int adc_total = 0;
145         int adc_retry_cnt = 0;
146         int i;
147
148         for (i = 0; i < SEC_JACK_SAMPLE_SIZE; i++) {
149
150         #if defined(CONFIG_STMPE811_ADC)
151                 adc_data = stmpe811_get_adc_data(SEC_JACK_ADC_CH);
152         #else
153                 adc_data = s3c_adc_read(padc, SEC_JACK_ADC_CH);
154         #endif
155
156                 if (adc_data < 0) {
157
158                         adc_retry_cnt++;
159
160                         if (adc_retry_cnt > 10)
161                                 return adc_data;
162                 }
163
164                 if (i != 0) {
165                         if (adc_data > adc_max)
166                                 adc_max = adc_data;
167                         else if (adc_data < adc_min)
168                                 adc_min = adc_data;
169                 } else {
170                         adc_max = adc_data;
171                         adc_min = adc_data;
172                 }
173                 adc_total += adc_data;
174         }
175
176         return (adc_total - adc_max - adc_min) / (SEC_JACK_SAMPLE_SIZE - 2);
177 }
178
179 /* gpio_input driver does not support to read adc value.
180  * We use input filter to support 3-buttons of headset
181  * without changing gpio_input driver.
182  */
183 static bool sec_jack_buttons_filter(struct input_handle *handle,
184                                     unsigned int type, unsigned int code,
185                                     int value)
186 {
187         struct sec_jack_info *hi = handle->handler->private;
188
189         if (hi->det_status == true)
190                 return false;
191
192         if (type != EV_KEY || code != KEY_UNKNOWN)
193                 return false;
194
195         hi->pressed = value;
196
197         /* This is called in timer handler of gpio_input driver.
198          * We use workqueue to read adc value.
199          */
200         queue_work(hi->queue, &hi->buttons_work);
201
202         return true;
203 }
204
205 static int sec_jack_buttons_connect(struct input_handler *handler,
206                                     struct input_dev *dev,
207                                     const struct input_device_id *id)
208 {
209         struct sec_jack_info *hi;
210         struct sec_jack_platform_data *pdata;
211         struct sec_jack_buttons_zone *btn_zones;
212         int err;
213         int i;
214
215         /* bind input_handler to input device related to only sec_jack */
216         if (dev->name != sec_jack_input_data.name)
217                 return -ENODEV;
218
219         hi = handler->private;
220         pdata = hi->pdata;
221         btn_zones = pdata->buttons_zones;
222
223         hi->input_dev = dev;
224         hi->handle.dev = dev;
225         hi->handle.handler = handler;
226         hi->handle.open = 0;
227         hi->handle.name = "sec_jack_buttons";
228
229         err = input_register_handle(&hi->handle);
230         if (err) {
231                 pr_err("%s: Failed to register sec_jack buttons handle, "
232                         "error %d\n", __func__, err);
233                 goto err_register_handle;
234         }
235
236         err = input_open_device(&hi->handle);
237         if (err) {
238                 pr_err("%s: Failed to open input device, error %d\n",
239                         __func__, err);
240                 goto err_open_device;
241         }
242
243         for (i = 0; i < pdata->num_buttons_zones; i++)
244                 input_set_capability(dev, EV_KEY, btn_zones[i].code);
245
246         return 0;
247
248  err_open_device:
249         input_unregister_handle(&hi->handle);
250  err_register_handle:
251
252         return err;
253 }
254
255 static void sec_jack_buttons_disconnect(struct input_handle *handle)
256 {
257         input_close_device(handle);
258         input_unregister_handle(handle);
259 }
260
261 static void sec_jack_set_type(struct sec_jack_info *hi, int jack_type)
262 {
263         struct sec_jack_platform_data *pdata = hi->pdata;
264
265         /* this can happen during slow inserts where we think we identified
266          * the type but then we get another interrupt and do it again
267          */
268         if (jack_type == hi->cur_jack_type) {
269                 if (jack_type != SEC_HEADSET_4POLE)
270                         pdata->set_micbias_state(false);
271
272                 return;
273         }
274
275         if (jack_type == SEC_HEADSET_4POLE) {
276                 /* for a 4 pole headset, enable detection of send/end key */
277                 if (hi->send_key_dev == NULL)
278                         /* enable to get events again */
279                         hi->send_key_dev = platform_device_register_data(NULL,
280                                         GPIO_EVENT_DEV_NAME,
281                                         hi->dev_id,
282                                         &sec_jack_input_data,
283                                         sizeof(sec_jack_input_data));
284         } else {
285                 /* for all other jacks, disable send/end key detection */
286                 if (hi->send_key_dev != NULL) {
287                         /* disable to prevent false events on next insert */
288                         platform_device_unregister(hi->send_key_dev);
289                         hi->send_key_dev = NULL;
290                 }
291                 /* micbias is left enabled for 4pole and disabled otherwise */
292                 pdata->set_micbias_state(false);
293         }
294         /* if user inserted ear jack slowly, different jack event can occur
295          * sometimes because irq_thread is defined IRQ_ONESHOT, detach status
296          * can be ignored sometimes so in that case, driver inform detach
297          * event to user side
298          */
299         switch_set_state(&switch_jack_detection, SEC_JACK_NO_DEVICE);
300
301         hi->cur_jack_type = jack_type;
302         pr_info("%s : jack_type = %d\n", __func__, jack_type);
303
304         switch_set_state(&switch_jack_detection, jack_type);
305 }
306
307 static void handle_jack_not_inserted(struct sec_jack_info *hi)
308 {
309         sec_jack_set_type(hi, SEC_JACK_NO_DEVICE);
310         hi->pdata->set_micbias_state(false);
311 }
312
313 static void determine_jack_type(struct sec_jack_info *hi)
314 {
315         struct sec_jack_platform_data *pdata = hi->pdata;
316         struct sec_jack_zone *zones = pdata->zones;
317         int size = pdata->num_zones;
318         int count[MAX_ZONE_LIMIT] = {0};
319         int adc;
320         int i;
321         unsigned npolarity = !pdata->det_active_high;
322
323         /* set mic bias to enable adc */
324         pdata->set_micbias_state(true);
325
326         while (gpio_get_value(pdata->det_gpio) ^ npolarity) {
327                 adc = sec_jack_get_adc_data(hi->padc);
328
329                 pr_debug("%s: adc = %d\n", __func__, adc);
330
331                 if (adc < 0)
332                         break;
333
334                 /* determine the type of headset based on the
335                  * adc value.  An adc value can fall in various
336                  * ranges or zones.  Within some ranges, the type
337                  * can be returned immediately.  Within others, the
338                  * value is considered unstable and we need to sample
339                  * a few more types (up to the limit determined by
340                  * the range) before we return the type for that range.
341                  */
342                 for (i = 0; i < size; i++) {
343                         if (adc <= zones[i].adc_high) {
344                                 if (++count[i] > zones[i].check_count) {
345                                         if (recheck_jack == true && i == 4) {
346                                                 pr_info("%s : something wrong connection!\n",
347                                                                 __func__);
348                                                 handle_jack_not_inserted(hi);
349
350                                                 recheck_jack = false;
351                                                 return;
352                                         }
353                                         sec_jack_set_type(hi,
354                                                 zones[i].jack_type);
355                                         return;
356                                 }
357                                 msleep(zones[i].delay_ms);
358                                 break;
359                         }
360                 }
361         }
362
363         recheck_jack = false;
364         /* jack removed before detection complete */
365         pr_debug("%s : jack removed before detection complete\n", __func__);
366         handle_jack_not_inserted(hi);
367 }
368
369 /* thread run whenever the headset detect state changes (either insertion
370  * or removal).
371  */
372 static irqreturn_t sec_jack_detect_irq_thread(int irq, void *dev_id)
373 {
374         struct sec_jack_info *hi = dev_id;
375         struct sec_jack_platform_data *pdata = hi->pdata;
376         unsigned npolarity = !pdata->det_active_high;
377         int curr_data;
378         int pre_data;
379         int loopcnt;
380         int check_loop_cnt = EAR_CHECK_LOOP_CNT;
381
382         hi->det_status = true;
383
384         /* prevent suspend to allow user space to respond to switch */
385         wake_lock_timeout(&hi->det_wake_lock, WAKE_LOCK_TIME);
386
387         /* debounce headset jack.  don't try to determine the type of
388          * headset until the detect state is true for a while.
389          */
390         pre_data = 0;
391         loopcnt = 0;
392         while (true) {
393                 curr_data = gpio_get_value(pdata->det_gpio);
394                 if (pre_data == curr_data)
395                         loopcnt++;
396                 else
397                         loopcnt = 0;
398
399                 pre_data = curr_data;
400
401                 if (loopcnt >= check_loop_cnt) {
402                         if (!curr_data ^ npolarity) {
403                                 /* jack not detected. */
404                                 handle_jack_not_inserted(hi);
405                                 hi->det_status = false;
406                                 return IRQ_HANDLED;
407                         }
408                         break;
409                 }
410                 msleep(20);
411         }
412
413         /* jack presence was detected the whole time, figure out which type */
414         determine_jack_type(hi);
415         hi->det_status = false;
416
417         return IRQ_HANDLED;
418 }
419
420 /* thread run whenever the button of headset is pressed or released */
421 void sec_jack_buttons_work(struct work_struct *work)
422 {
423         struct sec_jack_info *hi =
424                 container_of(work, struct sec_jack_info, buttons_work);
425         struct sec_jack_platform_data *pdata = hi->pdata;
426         struct sec_jack_buttons_zone *btn_zones = pdata->buttons_zones;
427         int adc;
428         int i;
429
430         /* prevent suspend to allow user space to respond to switch */
431         wake_lock_timeout(&hi->det_wake_lock, WAKE_LOCK_TIME);
432
433         /* when button is released */
434         if (hi->pressed == 0) {
435                 input_report_key(hi->input_dev, hi->pressed_code, 0);
436                 switch_set_state(&switch_sendend, 0);
437                 input_sync(hi->input_dev);
438                 pr_info("%s: earkey is released\n", __func__);
439                 pr_debug("keycode=%d\n", hi->pressed_code);
440                 return;
441         }
442
443         /* when button is pressed */
444         adc = sec_jack_get_adc_data(hi->padc);
445
446         for (i = 0; i < pdata->num_buttons_zones; i++)
447                 if (adc >= btn_zones[i].adc_low &&
448                     adc <= btn_zones[i].adc_high) {
449                         hi->pressed_code = btn_zones[i].code;
450                         input_report_key(hi->input_dev, btn_zones[i].code, 1);
451                         switch_set_state(&switch_sendend, 1);
452                         input_sync(hi->input_dev);
453                         pr_info("%s: earkey is pressed (adc:%d)\n",
454                                         __func__, adc);
455                         pr_debug("keycode=%d, is pressed\n", btn_zones[i].code);
456                         return;
457                 }
458
459         pr_warn("%s: key is skipped. ADC value is %d\n", __func__, adc);
460 }
461
462 static ssize_t select_jack_show(struct device *dev,
463         struct device_attribute *attr, char *buf)
464 {
465         pr_info("%s : operate nothing\n", __func__);
466
467         return 0;
468 }
469
470 static ssize_t select_jack_store(struct device *dev,
471         struct device_attribute *attr, const char *buf, size_t size)
472 {
473         struct sec_jack_info *hi = dev_get_drvdata(dev);
474         struct sec_jack_platform_data *pdata = hi->pdata;
475         int value = 0;
476
477
478         sscanf(buf, "%d", &value);
479         pr_err("%s: User  selection : 0X%x", __func__, value);
480         if (value == SEC_HEADSET_4POLE) {
481                 pdata->set_micbias_state(true);
482                 msleep(100);
483         }
484
485         sec_jack_set_type(hi, value);
486
487         return size;
488 }
489
490 #if defined(CONFIG_MACH_PX) || defined(CONFIG_MACH_P4NOTE) \
491         || defined(CONFIG_MACH_GC1)
492 static ssize_t earjack_key_state_show(struct device *dev,
493         struct device_attribute *attr, char *buf)
494 {
495         struct sec_jack_info *hi = dev_get_drvdata(dev);
496         int value = 0;
497
498         if (hi->pressed <= 0)
499                 value = 0;
500         else
501                 value = 1;
502
503         return sprintf(buf, "%d\n", value);
504 }
505
506 static ssize_t earjack_key_state_store(struct device *dev,
507         struct device_attribute *attr, const char *buf, size_t size)
508 {
509         pr_info("%s : operate nothing\n", __func__);
510
511         return size;
512 }
513
514 static ssize_t earjack_state_show(struct device *dev,
515         struct device_attribute *attr, char *buf)
516 {
517         struct sec_jack_info *hi = dev_get_drvdata(dev);
518         int value = 0;
519
520         if (hi->cur_jack_type == SEC_HEADSET_4POLE)
521                 value = 1;
522         else
523                 value = 0;
524
525         return sprintf(buf, "%d\n", value);
526 }
527
528 static ssize_t earjack_state_store(struct device *dev,
529         struct device_attribute *attr, const char *buf, size_t size)
530 {
531         pr_info("%s : operate nothing\n", __func__);
532
533         return size;
534 }
535 static DEVICE_ATTR(key_state, S_IRUGO | S_IWUSR | S_IWGRP,
536                    earjack_key_state_show, earjack_key_state_store);
537
538 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR | S_IWGRP,
539                    earjack_state_show, earjack_state_store);
540 #endif
541
542 static DEVICE_ATTR(select_jack, S_IRUGO | S_IWUSR | S_IWGRP,
543                 select_jack_show, select_jack_store);
544
545 static ssize_t reselect_jack_show(struct device *dev,
546         struct device_attribute *attr, char *buf)
547 {
548         pr_info("%s : operate nothing\n", __func__);
549
550         return 0;
551 }
552
553 static ssize_t reselect_jack_store(struct device *dev,
554         struct device_attribute *attr, const char *buf, size_t size)
555 {
556         struct sec_jack_info *hi = dev_get_drvdata(dev);
557         struct sec_jack_platform_data *pdata = hi->pdata;
558         int value = 0;
559
560
561         sscanf(buf, "%d", &value);
562         pr_err("%s: User reselection : 0X%x", __func__, value);
563
564         if (value == 1) {
565                 recheck_jack = true;
566                 determine_jack_type(hi);
567         }
568
569         return size;
570 }
571
572 static DEVICE_ATTR(reselect_jack, S_IRUGO | S_IWUSR | S_IWGRP,
573                 reselect_jack_show, reselect_jack_store);
574
575 static int sec_jack_probe(struct platform_device *pdev)
576 {
577         struct sec_jack_info *hi;
578         struct sec_jack_platform_data *pdata = pdev->dev.platform_data;
579         int ret;
580
581         pr_info("%s : Registering jack driver\n", __func__);
582         if (!pdata) {
583                 pr_err("%s : pdata is NULL.\n", __func__);
584                 return -ENODEV;
585         }
586
587         if (!pdata->zones
588                 || !pdata->set_micbias_state
589                 || pdata->num_zones > MAX_ZONE_LIMIT) {
590                 pr_err("%s : need to check pdata\n", __func__);
591                 return -ENODEV;
592         }
593
594         if (atomic_xchg(&instantiated, 1)) {
595                 pr_err("%s : already instantiated, can only have one\n",
596                         __func__);
597                 return -ENODEV;
598         }
599
600         sec_jack_key_map[0].gpio = pdata->send_end_gpio;
601
602         /* If no other keys in pdata, make all keys default to KEY_MEDIA */
603         if (pdata->num_buttons_zones == 0)
604                 sec_jack_key_map[0].code = KEY_MEDIA;
605
606         hi = kzalloc(sizeof(struct sec_jack_info), GFP_KERNEL);
607         if (hi == NULL) {
608                 pr_err("%s : Failed to allocate memory.\n", __func__);
609                 ret = -ENOMEM;
610                 goto err_kzalloc;
611         }
612
613         hi->pdata = pdata;
614
615         /* make the id of our gpio_event device the same as our platform device,
616          * which makes it the responsiblity of the board file to make sure
617          * it is unique relative to other gpio_event devices
618          */
619         hi->dev_id = pdev->id;
620
621         ret = gpio_request(pdata->det_gpio, "ear_jack_detect");
622         if (ret) {
623                 pr_err("%s : gpio_request failed for %d\n",
624                        __func__, pdata->det_gpio);
625                 goto err_gpio_request;
626         }
627
628         ret = switch_dev_register(&switch_jack_detection);
629         if (ret < 0) {
630                 pr_err("%s : Failed to register switch device\n", __func__);
631                 goto err_switch_dev_register;
632         }
633
634         ret = switch_dev_register(&switch_sendend);
635         if (ret < 0) {
636                 printk(KERN_ERR "SEC JACK: Failed to register switch device\n");
637                 goto err_switch_dev_register_send_end;
638         }
639         wake_lock_init(&hi->det_wake_lock, WAKE_LOCK_SUSPEND, "sec_jack_det");
640
641         INIT_WORK(&hi->buttons_work, sec_jack_buttons_work);
642         hi->queue = create_singlethread_workqueue("sec_jack_wq");
643         if (hi->queue == NULL) {
644                 ret = -ENOMEM;
645                 pr_err("%s: Failed to create workqueue\n", __func__);
646                 goto err_create_wq_failed;
647         }
648
649         hi->det_irq = gpio_to_irq(pdata->det_gpio);
650
651         jack_class = class_create(THIS_MODULE, JACK_CLASS_NAME);
652         if (IS_ERR(jack_class))
653                 pr_err("Failed to create class(sec_jack)\n");
654
655         /* support PBA function test */
656
657         jack_dev = device_create(jack_class, NULL, 0, hi, JACK_DEV_NAME);
658         if (IS_ERR(jack_dev))
659                 pr_err("Failed to create device(sec_jack)!= %ld\n",
660                         IS_ERR(jack_dev));
661
662         if (device_create_file(jack_dev, &dev_attr_select_jack) < 0)
663                 pr_err("Failed to create device file(%s)!\n",
664                         dev_attr_select_jack.attr.name);
665
666         jack_reselector = device_create(jack_class, NULL, 0, hi,
667                 JACK_RESELECTOR_NAME);
668         if (IS_ERR(jack_reselector))
669                 pr_err("Failed to create device(sec_jack)!= %ld\n",
670                         IS_ERR(jack_reselector));
671
672         if (device_create_file(jack_reselector, &dev_attr_reselect_jack) < 0)
673                 pr_err("Failed to create device file(%s)!\n",
674                         dev_attr_reselect_jack.attr.name);
675
676 #if defined(CONFIG_MACH_PX) || defined(CONFIG_MACH_P4NOTE) \
677         || defined(CONFIG_MACH_GC1)
678         if (device_create_file(jack_dev, &dev_attr_key_state) < 0)
679                 pr_err("Failed to create device file (%s)!\n",
680                         dev_attr_key_state.attr.name);
681
682         if (device_create_file(jack_dev, &dev_attr_state) < 0)
683                 pr_err("Failed to create device file (%s)!\n",
684                         dev_attr_state.attr.name);
685 #endif
686         set_bit(EV_KEY, hi->ids[0].evbit);
687         hi->ids[0].flags = INPUT_DEVICE_ID_MATCH_EVBIT;
688         hi->handler.filter = sec_jack_buttons_filter;
689         hi->handler.connect = sec_jack_buttons_connect;
690         hi->handler.disconnect = sec_jack_buttons_disconnect;
691         hi->handler.name = "sec_jack_buttons";
692         hi->handler.id_table = hi->ids;
693         hi->handler.private = hi;
694
695         /* Register adc client */
696         hi->padc = s3c_adc_register(pdev, NULL, NULL, 0);
697
698         if (IS_ERR(hi->padc)) {
699                 dev_err(&pdev->dev, "cannot register adc\n");
700                 ret = PTR_ERR(hi->padc);
701                 goto err_register_adc;
702         }
703
704         ret = input_register_handler(&hi->handler);
705         if (ret) {
706                 pr_err("%s : Failed to register_handler\n", __func__);
707                 goto err_register_input_handler;
708         }
709         ret = request_threaded_irq(hi->det_irq, NULL,
710                                    sec_jack_detect_irq_thread,
711                                    IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
712                                    IRQF_ONESHOT, "sec_headset_detect", hi);
713         if (ret) {
714                 pr_err("%s : Failed to request_irq.\n", __func__);
715                 goto err_request_detect_irq;
716         }
717
718         /* to handle insert/removal when we're sleeping in a call */
719         ret = enable_irq_wake(hi->det_irq);
720         if (ret) {
721                 pr_err("%s : Failed to enable_irq_wake.\n", __func__);
722                 goto err_enable_irq_wake;
723         }
724
725         dev_set_drvdata(&pdev->dev, hi);
726
727         /* Prove current earjack state */
728         determine_jack_type(hi);
729
730
731         return 0;
732
733 err_enable_irq_wake:
734         free_irq(hi->det_irq, hi);
735 err_request_detect_irq:
736         input_unregister_handler(&hi->handler);
737 err_register_input_handler:
738         s3c_adc_release(hi->padc);
739 err_register_adc:
740         destroy_workqueue(hi->queue);
741 err_create_wq_failed:
742         wake_lock_destroy(&hi->det_wake_lock);
743         switch_dev_unregister(&switch_sendend);
744 err_switch_dev_register_send_end:
745         switch_dev_unregister(&switch_jack_detection);
746 err_switch_dev_register:
747         gpio_free(pdata->det_gpio);
748 err_gpio_request:
749         kfree(hi);
750 err_kzalloc:
751         atomic_set(&instantiated, 0);
752
753         return ret;
754 }
755
756 static int sec_jack_remove(struct platform_device *pdev)
757 {
758
759         struct sec_jack_info *hi = dev_get_drvdata(&pdev->dev);
760
761         pr_info("%s :\n", __func__);
762         disable_irq_wake(hi->det_irq);
763         free_irq(hi->det_irq, hi);
764         destroy_workqueue(hi->queue);
765         if (hi->send_key_dev) {
766                 platform_device_unregister(hi->send_key_dev);
767                 hi->send_key_dev = NULL;
768         }
769         input_unregister_handler(&hi->handler);
770         wake_lock_destroy(&hi->det_wake_lock);
771         switch_dev_unregister(&switch_sendend);
772         switch_dev_unregister(&switch_jack_detection);
773         gpio_free(hi->pdata->det_gpio);
774         s3c_adc_release(hi->padc);
775         kfree(hi);
776         atomic_set(&instantiated, 0);
777
778         return 0;
779 }
780
781 static int sec_jack_suspend(struct device *dev)
782 {
783         struct sec_jack_info *hi = dev_get_drvdata(dev);
784         int ret;
785
786         ret = enable_irq_wake(hi->det_irq);
787
788         pr_info("%s: enable_irq_wake(%d)\n", __func__, ret);
789         disable_irq(hi->det_irq);
790
791         return 0;
792 }
793
794 static int sec_jack_resume(struct device *dev)
795 {
796         struct sec_jack_info *hi = dev_get_drvdata(dev);
797         int ret;
798
799         ret = disable_irq_wake(hi->det_irq);
800
801         pr_info("%s: disable_irq_wake(%d)\n", __func__, ret);
802         enable_irq(hi->det_irq);
803
804         return 0;
805 }
806
807 static const struct dev_pm_ops sec_jack_dev_pm_ops = {
808         .suspend        = sec_jack_suspend,
809         .resume         = sec_jack_resume,
810 };
811
812 static struct platform_driver sec_jack_driver = {
813         .probe  = sec_jack_probe,
814         .remove = sec_jack_remove,
815         .driver = {
816                 .name = "sec_jack",
817                 .owner = THIS_MODULE,
818                 .pm     = &sec_jack_dev_pm_ops,
819         },
820 };
821
822 static int __init sec_jack_init(void)
823 {
824         int ret;
825
826         ret =  platform_driver_register(&sec_jack_driver);
827
828         if (ret)
829                 pr_err("%s: Failed to add sec jack driver\n", __func__);
830
831         return ret;
832 }
833
834 static void __exit sec_jack_exit(void)
835 {
836         platform_driver_unregister(&sec_jack_driver);
837 }
838
839 module_init(sec_jack_init);
840 module_exit(sec_jack_exit);
841
842 MODULE_AUTHOR("ms17.kim@samsung.com");
843 MODULE_DESCRIPTION("Samsung Electronics Corp Ear-Jack detection driver");
844 MODULE_LICENSE("GPL");