1 /* drivers/misc/sec_jack.c
3 * Copyright (C) 2010 Samsung Electronics Co.Ltd
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.
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.
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>
36 #if defined(CONFIG_STMPE811_ADC)
37 #define SEC_JACK_ADC_CH 4
39 #define SEC_JACK_ADC_CH 3
41 #define SEC_JACK_SAMPLE_SIZE 5
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
53 #define SEND_KEY_CHECK_TIME_MS 40 /* 40ms */
55 #define WAKE_LOCK_TIME (HZ * 5) /* 5 sec */
56 #define EAR_CHECK_LOOP_CNT 10
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"
63 #define JACK_CLASS_NAME "jack"
64 #define JACK_DEV_NAME "jack_selector"
66 #define JACK_RESELECTOR_NAME "jack_reselector"
68 static struct class *jack_class;
69 static struct device *jack_dev;
70 static struct device *jack_reselector;
71 static bool recheck_jack;
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];
89 struct platform_device *send_key_dev;
90 unsigned int cur_jack_type;
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.
101 static atomic_t instantiated = ATOMIC_INIT(0);
103 /* sysfs name HeadsetObserver.java looks for to track headset state
105 struct switch_dev switch_jack_detection = {
109 /* To support AT+FCESTEST=1 */
110 struct switch_dev switch_sendend = {
114 static struct gpio_event_direct_entry sec_jack_key_map[] = {
120 static struct gpio_event_input_info sec_jack_key_info = {
121 .info.func = gpio_event_input_func,
122 .info.no_suspend = true,
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)
129 static struct gpio_event_info *sec_jack_input_info[] = {
130 &sec_jack_key_info.info,
133 static struct gpio_event_platform_data sec_jack_input_data = {
135 .info = sec_jack_input_info,
136 .info_count = ARRAY_SIZE(sec_jack_input_info),
139 static int sec_jack_get_adc_data(struct s3c_adc_client *padc)
143 int adc_min = 0xFFFF;
145 int adc_retry_cnt = 0;
148 for (i = 0; i < SEC_JACK_SAMPLE_SIZE; i++) {
150 #if defined(CONFIG_STMPE811_ADC)
151 adc_data = stmpe811_get_adc_data(SEC_JACK_ADC_CH);
153 adc_data = s3c_adc_read(padc, SEC_JACK_ADC_CH);
160 if (adc_retry_cnt > 10)
165 if (adc_data > adc_max)
167 else if (adc_data < adc_min)
173 adc_total += adc_data;
176 return (adc_total - adc_max - adc_min) / (SEC_JACK_SAMPLE_SIZE - 2);
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.
183 static bool sec_jack_buttons_filter(struct input_handle *handle,
184 unsigned int type, unsigned int code,
187 struct sec_jack_info *hi = handle->handler->private;
189 if (hi->det_status == true)
192 if (type != EV_KEY || code != KEY_UNKNOWN)
197 /* This is called in timer handler of gpio_input driver.
198 * We use workqueue to read adc value.
200 queue_work(hi->queue, &hi->buttons_work);
205 static int sec_jack_buttons_connect(struct input_handler *handler,
206 struct input_dev *dev,
207 const struct input_device_id *id)
209 struct sec_jack_info *hi;
210 struct sec_jack_platform_data *pdata;
211 struct sec_jack_buttons_zone *btn_zones;
215 /* bind input_handler to input device related to only sec_jack */
216 if (dev->name != sec_jack_input_data.name)
219 hi = handler->private;
221 btn_zones = pdata->buttons_zones;
224 hi->handle.dev = dev;
225 hi->handle.handler = handler;
227 hi->handle.name = "sec_jack_buttons";
229 err = input_register_handle(&hi->handle);
231 pr_err("%s: Failed to register sec_jack buttons handle, "
232 "error %d\n", __func__, err);
233 goto err_register_handle;
236 err = input_open_device(&hi->handle);
238 pr_err("%s: Failed to open input device, error %d\n",
240 goto err_open_device;
243 for (i = 0; i < pdata->num_buttons_zones; i++)
244 input_set_capability(dev, EV_KEY, btn_zones[i].code);
249 input_unregister_handle(&hi->handle);
255 static void sec_jack_buttons_disconnect(struct input_handle *handle)
257 input_close_device(handle);
258 input_unregister_handle(handle);
261 static void sec_jack_set_type(struct sec_jack_info *hi, int jack_type)
263 struct sec_jack_platform_data *pdata = hi->pdata;
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
268 if (jack_type == hi->cur_jack_type) {
269 if (jack_type != SEC_HEADSET_4POLE)
270 pdata->set_micbias_state(false);
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,
282 &sec_jack_input_data,
283 sizeof(sec_jack_input_data));
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;
291 /* micbias is left enabled for 4pole and disabled otherwise */
292 pdata->set_micbias_state(false);
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
299 switch_set_state(&switch_jack_detection, SEC_JACK_NO_DEVICE);
301 hi->cur_jack_type = jack_type;
302 pr_info("%s : jack_type = %d\n", __func__, jack_type);
304 switch_set_state(&switch_jack_detection, jack_type);
307 static void handle_jack_not_inserted(struct sec_jack_info *hi)
309 sec_jack_set_type(hi, SEC_JACK_NO_DEVICE);
310 hi->pdata->set_micbias_state(false);
313 static void determine_jack_type(struct sec_jack_info *hi)
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};
321 unsigned npolarity = !pdata->det_active_high;
323 /* set mic bias to enable adc */
324 pdata->set_micbias_state(true);
326 while (gpio_get_value(pdata->det_gpio) ^ npolarity) {
327 adc = sec_jack_get_adc_data(hi->padc);
329 pr_debug("%s: adc = %d\n", __func__, adc);
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.
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",
348 handle_jack_not_inserted(hi);
350 recheck_jack = false;
353 sec_jack_set_type(hi,
357 msleep(zones[i].delay_ms);
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);
369 /* thread run whenever the headset detect state changes (either insertion
372 static irqreturn_t sec_jack_detect_irq_thread(int irq, void *dev_id)
374 struct sec_jack_info *hi = dev_id;
375 struct sec_jack_platform_data *pdata = hi->pdata;
376 unsigned npolarity = !pdata->det_active_high;
380 int check_loop_cnt = EAR_CHECK_LOOP_CNT;
382 hi->det_status = true;
384 /* prevent suspend to allow user space to respond to switch */
385 wake_lock_timeout(&hi->det_wake_lock, WAKE_LOCK_TIME);
387 /* debounce headset jack. don't try to determine the type of
388 * headset until the detect state is true for a while.
393 curr_data = gpio_get_value(pdata->det_gpio);
394 if (pre_data == curr_data)
399 pre_data = curr_data;
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;
413 /* jack presence was detected the whole time, figure out which type */
414 determine_jack_type(hi);
415 hi->det_status = false;
420 /* thread run whenever the button of headset is pressed or released */
421 void sec_jack_buttons_work(struct work_struct *work)
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;
430 /* prevent suspend to allow user space to respond to switch */
431 wake_lock_timeout(&hi->det_wake_lock, WAKE_LOCK_TIME);
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);
443 /* when button is pressed */
444 adc = sec_jack_get_adc_data(hi->padc);
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",
455 pr_debug("keycode=%d, is pressed\n", btn_zones[i].code);
459 pr_warn("%s: key is skipped. ADC value is %d\n", __func__, adc);
462 static ssize_t select_jack_show(struct device *dev,
463 struct device_attribute *attr, char *buf)
465 pr_info("%s : operate nothing\n", __func__);
470 static ssize_t select_jack_store(struct device *dev,
471 struct device_attribute *attr, const char *buf, size_t size)
473 struct sec_jack_info *hi = dev_get_drvdata(dev);
474 struct sec_jack_platform_data *pdata = hi->pdata;
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);
485 sec_jack_set_type(hi, value);
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)
495 struct sec_jack_info *hi = dev_get_drvdata(dev);
498 if (hi->pressed <= 0)
503 return sprintf(buf, "%d\n", value);
506 static ssize_t earjack_key_state_store(struct device *dev,
507 struct device_attribute *attr, const char *buf, size_t size)
509 pr_info("%s : operate nothing\n", __func__);
514 static ssize_t earjack_state_show(struct device *dev,
515 struct device_attribute *attr, char *buf)
517 struct sec_jack_info *hi = dev_get_drvdata(dev);
520 if (hi->cur_jack_type == SEC_HEADSET_4POLE)
525 return sprintf(buf, "%d\n", value);
528 static ssize_t earjack_state_store(struct device *dev,
529 struct device_attribute *attr, const char *buf, size_t size)
531 pr_info("%s : operate nothing\n", __func__);
535 static DEVICE_ATTR(key_state, S_IRUGO | S_IWUSR | S_IWGRP,
536 earjack_key_state_show, earjack_key_state_store);
538 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR | S_IWGRP,
539 earjack_state_show, earjack_state_store);
542 static DEVICE_ATTR(select_jack, S_IRUGO | S_IWUSR | S_IWGRP,
543 select_jack_show, select_jack_store);
545 static ssize_t reselect_jack_show(struct device *dev,
546 struct device_attribute *attr, char *buf)
548 pr_info("%s : operate nothing\n", __func__);
553 static ssize_t reselect_jack_store(struct device *dev,
554 struct device_attribute *attr, const char *buf, size_t size)
556 struct sec_jack_info *hi = dev_get_drvdata(dev);
557 struct sec_jack_platform_data *pdata = hi->pdata;
561 sscanf(buf, "%d", &value);
562 pr_err("%s: User reselection : 0X%x", __func__, value);
566 determine_jack_type(hi);
572 static DEVICE_ATTR(reselect_jack, S_IRUGO | S_IWUSR | S_IWGRP,
573 reselect_jack_show, reselect_jack_store);
575 static int sec_jack_probe(struct platform_device *pdev)
577 struct sec_jack_info *hi;
578 struct sec_jack_platform_data *pdata = pdev->dev.platform_data;
581 pr_info("%s : Registering jack driver\n", __func__);
583 pr_err("%s : pdata is NULL.\n", __func__);
588 || !pdata->set_micbias_state
589 || pdata->num_zones > MAX_ZONE_LIMIT) {
590 pr_err("%s : need to check pdata\n", __func__);
594 if (atomic_xchg(&instantiated, 1)) {
595 pr_err("%s : already instantiated, can only have one\n",
600 sec_jack_key_map[0].gpio = pdata->send_end_gpio;
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;
606 hi = kzalloc(sizeof(struct sec_jack_info), GFP_KERNEL);
608 pr_err("%s : Failed to allocate memory.\n", __func__);
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
619 hi->dev_id = pdev->id;
621 ret = gpio_request(pdata->det_gpio, "ear_jack_detect");
623 pr_err("%s : gpio_request failed for %d\n",
624 __func__, pdata->det_gpio);
625 goto err_gpio_request;
628 ret = switch_dev_register(&switch_jack_detection);
630 pr_err("%s : Failed to register switch device\n", __func__);
631 goto err_switch_dev_register;
634 ret = switch_dev_register(&switch_sendend);
636 printk(KERN_ERR "SEC JACK: Failed to register switch device\n");
637 goto err_switch_dev_register_send_end;
639 wake_lock_init(&hi->det_wake_lock, WAKE_LOCK_SUSPEND, "sec_jack_det");
641 INIT_WORK(&hi->buttons_work, sec_jack_buttons_work);
642 hi->queue = create_singlethread_workqueue("sec_jack_wq");
643 if (hi->queue == NULL) {
645 pr_err("%s: Failed to create workqueue\n", __func__);
646 goto err_create_wq_failed;
649 hi->det_irq = gpio_to_irq(pdata->det_gpio);
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");
655 /* support PBA function test */
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",
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);
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));
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);
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);
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);
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;
695 /* Register adc client */
696 hi->padc = s3c_adc_register(pdev, NULL, NULL, 0);
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;
704 ret = input_register_handler(&hi->handler);
706 pr_err("%s : Failed to register_handler\n", __func__);
707 goto err_register_input_handler;
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);
714 pr_err("%s : Failed to request_irq.\n", __func__);
715 goto err_request_detect_irq;
718 /* to handle insert/removal when we're sleeping in a call */
719 ret = enable_irq_wake(hi->det_irq);
721 pr_err("%s : Failed to enable_irq_wake.\n", __func__);
722 goto err_enable_irq_wake;
725 dev_set_drvdata(&pdev->dev, hi);
727 /* Prove current earjack state */
728 determine_jack_type(hi);
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);
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);
751 atomic_set(&instantiated, 0);
756 static int sec_jack_remove(struct platform_device *pdev)
759 struct sec_jack_info *hi = dev_get_drvdata(&pdev->dev);
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;
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);
776 atomic_set(&instantiated, 0);
781 static int sec_jack_suspend(struct device *dev)
783 struct sec_jack_info *hi = dev_get_drvdata(dev);
786 ret = enable_irq_wake(hi->det_irq);
788 pr_info("%s: enable_irq_wake(%d)\n", __func__, ret);
789 disable_irq(hi->det_irq);
794 static int sec_jack_resume(struct device *dev)
796 struct sec_jack_info *hi = dev_get_drvdata(dev);
799 ret = disable_irq_wake(hi->det_irq);
801 pr_info("%s: disable_irq_wake(%d)\n", __func__, ret);
802 enable_irq(hi->det_irq);
807 static const struct dev_pm_ops sec_jack_dev_pm_ops = {
808 .suspend = sec_jack_suspend,
809 .resume = sec_jack_resume,
812 static struct platform_driver sec_jack_driver = {
813 .probe = sec_jack_probe,
814 .remove = sec_jack_remove,
817 .owner = THIS_MODULE,
818 .pm = &sec_jack_dev_pm_ops,
822 static int __init sec_jack_init(void)
826 ret = platform_driver_register(&sec_jack_driver);
829 pr_err("%s: Failed to add sec jack driver\n", __func__);
834 static void __exit sec_jack_exit(void)
836 platform_driver_unregister(&sec_jack_driver);
839 module_init(sec_jack_init);
840 module_exit(sec_jack_exit);
842 MODULE_AUTHOR("ms17.kim@samsung.com");
843 MODULE_DESCRIPTION("Samsung Electronics Corp Ear-Jack detection driver");
844 MODULE_LICENSE("GPL");