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>
34 #ifdef CONFIG_SOUND_CONTROL
35 #include <linux/sound_control.h>
38 #define MAX_ZONE_LIMIT 10
39 #define SEND_KEY_CHECK_TIME_MS 30 /* 30ms */
40 #define DET_CHECK_TIME_MS 200 /* 200ms */
41 #define WAKE_LOCK_TIME (HZ * 5) /* 5 sec */
43 struct sec_jack_info
{
44 struct sec_jack_platform_data
*pdata
;
45 struct delayed_work jack_detect_work
;
46 struct work_struct buttons_work
;
47 struct work_struct detect_work
;
48 struct workqueue_struct
*queue
;
49 struct input_dev
*input_dev
;
50 struct wake_lock det_wake_lock
;
51 struct sec_jack_zone
*zone
;
52 struct input_handler handler
;
53 struct input_handle handle
;
54 struct input_device_id ids
;
59 struct platform_device
*send_key_dev
;
60 unsigned int cur_jack_type
;
63 /* with some modifications like moving all the gpio structs inside
64 * the platform data and getting the name for the switch and
65 * gpio_event from the platform data, the driver could support more than
66 * one headset jack, but currently user space is looking only for
67 * one key file and switch for a headset so it'd be overkill and
68 * untestable so we limit to one instantiation for now.
70 static atomic_t instantiated
= ATOMIC_INIT(0);
72 /* sysfs name HeadsetObserver.java looks for to track headset state
74 struct switch_dev switch_jack_detection
= {
78 static struct gpio_event_direct_entry sec_jack_key_map
[] = {
84 static struct gpio_event_input_info sec_jack_key_info
= {
85 .info
.func
= gpio_event_input_func
,
86 .info
.no_suspend
= true,
88 .debounce_time
.tv64
= SEND_KEY_CHECK_TIME_MS
* NSEC_PER_MSEC
,
89 .keymap
= sec_jack_key_map
,
90 .keymap_size
= ARRAY_SIZE(sec_jack_key_map
)
93 static struct gpio_event_info
*sec_jack_input_info
[] = {
94 &sec_jack_key_info
.info
,
97 static struct gpio_event_platform_data sec_jack_input_data
= {
99 .info
= sec_jack_input_info
,
100 .info_count
= ARRAY_SIZE(sec_jack_input_info
),
103 /* gpio_input driver does not support to read adc value.
104 * We use input filter to support 3-buttons of headset
105 * without changing gpio_input driver.
107 static bool sec_jack_buttons_filter(struct input_handle
*handle
,
108 unsigned int type
, unsigned int code
,
111 struct sec_jack_info
*hi
= handle
->handler
->private;
113 if (type
!= EV_KEY
|| code
!= KEY_UNKNOWN
)
118 /* This is called in timer handler of gpio_input driver.
119 * We use workqueue to read adc value.
121 queue_work(hi
->queue
, &hi
->buttons_work
);
126 static int sec_jack_buttons_connect(struct input_handler
*handler
,
127 struct input_dev
*dev
,
128 const struct input_device_id
*id
)
130 struct sec_jack_info
*hi
;
131 struct sec_jack_platform_data
*pdata
;
132 struct sec_jack_buttons_zone
*btn_zones
;
136 /* bind input_handler to input device related to only sec_jack */
137 if (dev
->name
!= sec_jack_input_data
.name
)
140 hi
= handler
->private;
142 btn_zones
= pdata
->buttons_zones
;
145 hi
->handle
.dev
= dev
;
146 hi
->handle
.handler
= handler
;
148 hi
->handle
.name
= "sec_jack_buttons";
150 err
= input_register_handle(&hi
->handle
);
152 pr_err("%s: Failed to register sec_jack buttons handle, "
153 "error %d\n", __func__
, err
);
154 goto err_register_handle
;
157 err
= input_open_device(&hi
->handle
);
159 pr_err("%s: Failed to open input device, error %d\n",
161 goto err_open_device
;
164 for (i
= 0; i
< pdata
->num_buttons_zones
; i
++)
165 input_set_capability(dev
, EV_KEY
, btn_zones
[i
].code
);
170 input_unregister_handle(&hi
->handle
);
176 static void sec_jack_buttons_disconnect(struct input_handle
*handle
)
178 input_close_device(handle
);
179 input_unregister_handle(handle
);
182 static void sec_jack_set_type(struct sec_jack_info
*hi
, int jack_type
)
184 struct sec_jack_platform_data
*pdata
= hi
->pdata
;
186 /* this can happen during slow inserts where we think we identified
187 * the type but then we get another interrupt and do it again
189 if (jack_type
== hi
->cur_jack_type
) {
190 if (jack_type
!= SEC_HEADSET_4POLE
)
191 pdata
->set_micbias_state(false);
195 #ifdef CONFIG_SOUND_CONTROL
196 soundcontrol_reportjack(jack_type
);
199 if (jack_type
== SEC_HEADSET_4POLE
) {
200 /* for a 4 pole headset, enable detection of send/end key */
201 if (hi
->send_key_dev
== NULL
)
202 /* enable to get events again */
203 hi
->send_key_dev
= platform_device_register_data(NULL
,
206 &sec_jack_input_data
,
207 sizeof(sec_jack_input_data
));
209 /* for all other jacks, disable send/end key detection */
210 if (hi
->send_key_dev
!= NULL
) {
211 /* disable to prevent false events on next insert */
212 platform_device_unregister(hi
->send_key_dev
);
213 hi
->send_key_dev
= NULL
;
215 /* micbias is left enabled for 4pole and disabled otherwise */
216 pdata
->set_micbias_state(false);
219 hi
->cur_jack_type
= jack_type
;
220 pr_info("%s : jack_type = %d\n", __func__
, jack_type
);
222 /* prevent suspend to allow user space to respond to switch */
223 wake_lock_timeout(&hi
->det_wake_lock
, WAKE_LOCK_TIME
);
225 switch_set_state(&switch_jack_detection
, jack_type
);
228 static void handle_jack_not_inserted(struct sec_jack_info
*hi
)
230 sec_jack_set_type(hi
, SEC_JACK_NO_DEVICE
);
231 hi
->pdata
->set_micbias_state(false);
234 static void determine_jack_type(struct sec_jack_info
*hi
)
236 struct sec_jack_zone
*zones
= hi
->pdata
->zones
;
237 int size
= hi
->pdata
->num_zones
;
238 int count
[MAX_ZONE_LIMIT
] = {0};
241 unsigned npolarity
= !hi
->pdata
->det_active_high
;
243 while (gpio_get_value(hi
->pdata
->det_gpio
) ^ npolarity
) {
244 adc
= hi
->pdata
->get_adc_value();
245 pr_debug("%s: adc = %d\n", __func__
, adc
);
247 /* determine the type of headset based on the
248 * adc value. An adc value can fall in various
249 * ranges or zones. Within some ranges, the type
250 * can be returned immediately. Within others, the
251 * value is considered unstable and we need to sample
252 * a few more types (up to the limit determined by
253 * the range) before we return the type for that range.
255 for (i
= 0; i
< size
; i
++) {
256 if (adc
<= zones
[i
].adc_high
) {
257 if (++count
[i
] > zones
[i
].check_count
) {
258 sec_jack_set_type(hi
,
262 msleep(zones
[i
].delay_ms
);
267 /* jack removed before detection complete */
268 pr_debug("%s : jack removed before detection complete\n", __func__
);
269 handle_jack_not_inserted(hi
);
272 /* thread run whenever the headset detect state changes (either insertion
275 static irqreturn_t
sec_jack_detect_irq(int irq
, void *dev_id
)
277 struct sec_jack_info
*hi
= dev_id
;
279 queue_work(hi
->queue
, &hi
->detect_work
);
284 void sec_jack_detect_work(struct work_struct
*work
)
286 struct sec_jack_info
*hi
=
287 container_of(work
, struct sec_jack_info
, detect_work
);
288 struct sec_jack_platform_data
*pdata
= hi
->pdata
;
289 int time_left_ms
= DET_CHECK_TIME_MS
;
290 unsigned npolarity
= !hi
->pdata
->det_active_high
;
292 /* set mic bias to enable adc */
293 pdata
->set_micbias_state(true);
295 /* debounce headset jack. don't try to determine the type of
296 * headset until the detect state is true for a while.
298 while (time_left_ms
> 0) {
299 if (!(gpio_get_value(hi
->pdata
->det_gpio
) ^ npolarity
)) {
300 /* jack not detected. */
301 handle_jack_not_inserted(hi
);
307 /* jack presence was detected the whole time, figure out which type */
308 determine_jack_type(hi
);
311 /* thread run whenever the button of headset is pressed or released */
312 void sec_jack_buttons_work(struct work_struct
*work
)
314 struct sec_jack_info
*hi
=
315 container_of(work
, struct sec_jack_info
, buttons_work
);
316 struct sec_jack_platform_data
*pdata
= hi
->pdata
;
317 struct sec_jack_buttons_zone
*btn_zones
= pdata
->buttons_zones
;
321 /* when button is released */
322 if (hi
->pressed
== 0) {
323 input_report_key(hi
->input_dev
, hi
->pressed_code
, 0);
324 input_sync(hi
->input_dev
);
325 pr_debug("%s: keycode=%d, is released\n", __func__
,
330 /* when button is pressed */
331 adc
= pdata
->get_adc_value();
333 for (i
= 0; i
< pdata
->num_buttons_zones
; i
++)
334 if (adc
>= btn_zones
[i
].adc_low
&&
335 adc
<= btn_zones
[i
].adc_high
) {
336 hi
->pressed_code
= btn_zones
[i
].code
;
337 input_report_key(hi
->input_dev
, btn_zones
[i
].code
, 1);
338 input_sync(hi
->input_dev
);
339 pr_debug("%s: keycode=%d, is pressed\n", __func__
,
344 pr_warn("%s: key is skipped. ADC value is %d\n", __func__
, adc
);
347 static int sec_jack_probe(struct platform_device
*pdev
)
349 struct sec_jack_info
*hi
;
350 struct sec_jack_platform_data
*pdata
= pdev
->dev
.platform_data
;
353 pr_info("%s : Registering jack driver\n", __func__
);
355 pr_err("%s : pdata is NULL.\n", __func__
);
359 if (!pdata
->get_adc_value
|| !pdata
->zones
||
360 !pdata
->set_micbias_state
|| pdata
->num_zones
> MAX_ZONE_LIMIT
) {
361 pr_err("%s : need to check pdata\n", __func__
);
365 if (atomic_xchg(&instantiated
, 1)) {
366 pr_err("%s : already instantiated, can only have one\n",
371 sec_jack_key_map
[0].gpio
= pdata
->send_end_gpio
;
373 hi
= kzalloc(sizeof(struct sec_jack_info
), GFP_KERNEL
);
375 pr_err("%s : Failed to allocate memory.\n", __func__
);
382 /* make the id of our gpi_event device the same as our platform device,
383 * which makes it the responsiblity of the board file to make sure
384 * it is unique relative to other gpio_event devices
386 hi
->dev_id
= pdev
->id
;
388 ret
= gpio_request(pdata
->det_gpio
, "ear_jack_detect");
390 pr_err("%s : gpio_request failed for %d\n",
391 __func__
, pdata
->det_gpio
);
392 goto err_gpio_request
;
395 ret
= switch_dev_register(&switch_jack_detection
);
397 pr_err("%s : Failed to register switch device\n", __func__
);
398 goto err_switch_dev_register
;
401 wake_lock_init(&hi
->det_wake_lock
, WAKE_LOCK_SUSPEND
, "sec_jack_det");
403 INIT_WORK(&hi
->buttons_work
, sec_jack_buttons_work
);
404 INIT_WORK(&hi
->detect_work
, sec_jack_detect_work
);
405 hi
->queue
= create_freezable_workqueue("sec_jack_wq");
406 if (hi
->queue
== NULL
) {
408 pr_err("%s: Failed to create workqueue\n", __func__
);
409 goto err_create_wq_failed
;
411 queue_work(hi
->queue
, &hi
->detect_work
);
413 hi
->det_irq
= gpio_to_irq(pdata
->det_gpio
);
415 set_bit(EV_KEY
, hi
->ids
.evbit
);
416 hi
->ids
.flags
= INPUT_DEVICE_ID_MATCH_EVBIT
;
417 hi
->handler
.filter
= sec_jack_buttons_filter
;
418 hi
->handler
.connect
= sec_jack_buttons_connect
;
419 hi
->handler
.disconnect
= sec_jack_buttons_disconnect
;
420 hi
->handler
.name
= "sec_jack_buttons";
421 hi
->handler
.id_table
= &hi
->ids
;
422 hi
->handler
.private = hi
;
424 ret
= input_register_handler(&hi
->handler
);
426 pr_err("%s : Failed to register_handler\n", __func__
);
427 goto err_register_input_handler
;
429 ret
= request_irq(hi
->det_irq
, sec_jack_detect_irq
,
430 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
|
431 IRQF_ONESHOT
, "sec_headset_detect", hi
);
433 pr_err("%s : Failed to request_irq.\n", __func__
);
434 goto err_request_detect_irq
;
437 /* to handle insert/removal when we're sleeping in a call */
438 ret
= enable_irq_wake(hi
->det_irq
);
440 pr_err("%s : Failed to enable_irq_wake.\n", __func__
);
441 goto err_enable_irq_wake
;
444 dev_set_drvdata(&pdev
->dev
, hi
);
449 free_irq(hi
->det_irq
, hi
);
450 err_request_detect_irq
:
451 input_unregister_handler(&hi
->handler
);
452 err_register_input_handler
:
453 destroy_workqueue(hi
->queue
);
454 err_create_wq_failed
:
455 wake_lock_destroy(&hi
->det_wake_lock
);
456 switch_dev_unregister(&switch_jack_detection
);
457 err_switch_dev_register
:
458 gpio_free(pdata
->det_gpio
);
462 atomic_set(&instantiated
, 0);
467 static int sec_jack_remove(struct platform_device
*pdev
)
470 struct sec_jack_info
*hi
= dev_get_drvdata(&pdev
->dev
);
472 pr_info("%s :\n", __func__
);
473 disable_irq_wake(hi
->det_irq
);
474 free_irq(hi
->det_irq
, hi
);
475 destroy_workqueue(hi
->queue
);
476 if (hi
->send_key_dev
) {
477 platform_device_unregister(hi
->send_key_dev
);
478 hi
->send_key_dev
= NULL
;
480 input_unregister_handler(&hi
->handler
);
481 wake_lock_destroy(&hi
->det_wake_lock
);
482 switch_dev_unregister(&switch_jack_detection
);
483 gpio_free(hi
->pdata
->det_gpio
);
485 atomic_set(&instantiated
, 0);
490 static struct platform_driver sec_jack_driver
= {
491 .probe
= sec_jack_probe
,
492 .remove
= sec_jack_remove
,
495 .owner
= THIS_MODULE
,
498 static int __init
sec_jack_init(void)
500 return platform_driver_register(&sec_jack_driver
);
503 static void __exit
sec_jack_exit(void)
505 platform_driver_unregister(&sec_jack_driver
);
508 module_init(sec_jack_init
);
509 module_exit(sec_jack_exit
);
511 MODULE_AUTHOR("ms17.kim@samsung.com");
512 MODULE_DESCRIPTION("Samsung Electronics Corp Ear-Jack detection driver");
513 MODULE_LICENSE("GPL");