aboutsummaryrefslogtreecommitdiff
path: root/drivers/switch/hds_fsa8008.c
blob: 7506c9fd9a9dd5345544e2fa82d0e8206deab873 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/* drivers/switch/hds_fsa8008.c
 *
 *  LGE 3.5 PI Headset detection driver using fsa8008.
 *
 * Copyright (C) 2008 Google, Inc.
 * Author: Mike Lockwood <lockwood@android.com>
 *
 * Copyright (C) 2009-2012 LGE, Inc.
 * Lee SungYoung <lsy@lge.com>
 * Kim Eun Hye <ehgrace.kim@lge.com>
 * Yoon Gi Souk <gisouk.yoon@lge.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

/* Interface is following:
 * android:frameworks/base/services/java/com/android/server/HeadsetObserver.java
 * HEADSET_UEVENT_MATCH = "DEVPATH=/sys/devices/virtual/switch/h2w"
 * HEADSET_STATE_PATH = /sys/class/switch/h2w/state
 * HEADSET_NAME_PATH = /sys/class/switch/h2w/name
 */
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/switch.h>
#include <linux/workqueue.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/irq.h>
#include <linux/mutex.h>
#include <linux/hrtimer.h>
#include <linux/input.h>
#include <linux/debugfs.h>
#include <linux/wakelock.h>
#include <linux/platform_data/hds_fsa8008.h>

#define FSA8008_USE_WORK_QUEUE
#define FSA8008_KEY_LATENCY_TIME	200 /* in ms */
#define FSA8008_DEBOUNCE_TIME		500 /* in ms */
#define FSA8008_WAKELOCK_TIMEOUT	(2*HZ)

#define HSD_DEBUG_PRINT

#ifdef HSD_DEBUG_PRINT
#define HSD_DBG(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __func__, ##args)
#else
#define HSD_DBG(fmt, args...) do {} while (0)
#endif

#ifdef FSA8008_USE_WORK_QUEUE
static struct workqueue_struct *local_fsa8008_workqueue;
#endif

static struct wake_lock ear_hook_wake_lock;

struct hsd_info {
	/* function devices provided by this driver */
	struct switch_dev sdev;
	struct input_dev *input;
	/* mutex */
	struct mutex mutex_lock;
	/* h/w configuration : initilized by platform data */
	unsigned int gpio_detect; /* DET : to detect jack inserted or not */
	unsigned int gpio_detect_can_wakeup;
	unsigned int gpio_mic_en; /* EN : to enable mic */
	unsigned int gpio_mic_bias_en; /* EN : to enable mic bias */
	unsigned int gpio_jpole;  /* JPOLE : 3pole or 4pole */
	unsigned int gpio_key;    /* S/E button */

	/* callback function which is initialized while probing */
	void (*set_headset_mic_bias)(int enable);
	void (*set_uart_console)(int enable);

	unsigned int latency_for_detection;
	unsigned int latency_for_key;

	unsigned int key_code;

	/* irqs */
	unsigned int irq_detect;
	unsigned int irq_key;
	/* internal states */
	atomic_t irq_key_enabled;
	atomic_t is_3_pole_or_not;
	atomic_t btn_state;
	int saved_detect;
	/* work for detect_work */
	struct delayed_work work;
	struct delayed_work work_for_key_pressed;
	struct delayed_work work_for_key_released;
};

enum {
	NO_DEVICE   = 0,
	HEADSET_WITH_MIC = (1 << 0),
	HEADSET_NO_MIC = (1 << 1),
};
enum {
	HEADSET_INSERT = 0,
	HEADSET_REMOVE = 1,
};

enum {
	HEADSET_4POLE = 0,
	HEADSET_3POLE = 1,
};

static ssize_t hsd_print_name(struct switch_dev *sdev, char *buf)
{
	switch (switch_get_state(sdev)) {
	case NO_DEVICE:
		return sprintf(buf, "No Device\n");
	case HEADSET_WITH_MIC:
		return sprintf(buf, "Headset\n");
	case HEADSET_NO_MIC:
		return sprintf(buf, "Headset_no_mic\n");

	}
	return -EINVAL;
}

static ssize_t hsd_print_state(struct switch_dev *sdev, char *buf)
{
	return sprintf(buf, "%d\n", switch_get_state(sdev));
}

static void button_pressed(struct work_struct *work)
{
	struct delayed_work *dwork = container_of(work, struct delayed_work, work);
	struct hsd_info *hi = container_of(dwork, struct hsd_info, work_for_key_pressed);


	if (gpio_get_value_cansleep(hi->gpio_detect) &&
			(switch_get_state(&hi->sdev)== HEADSET_WITH_MIC)) {
		pr_warn("%s: ear jack was plugged out already!"
			"just ignore the event.\n", __func__);
		return;
	}

	HSD_DBG("button_pressed \n");

	atomic_set(&hi->btn_state, 1);
	input_report_key(hi->input, hi->key_code, 1);
	input_sync(hi->input);
}

static void button_released(struct work_struct *work)
{
	struct delayed_work *dwork = container_of(
			work, struct delayed_work, work);
	struct hsd_info *hi = container_of(
			dwork, struct hsd_info, work_for_key_released);

	if (gpio_get_value_cansleep(hi->gpio_detect) &&
			(switch_get_state(&hi->sdev)== HEADSET_WITH_MIC)){
		pr_warn("%s: ear jack was plugged out already!"
			"just ignore the event.\n", __func__);
		return;
	}

	HSD_DBG("button_released \n");

	atomic_set(&hi->btn_state, 0);
	input_report_key(hi->input, hi->key_code, 0);
	input_sync(hi->input);
}

static void insert_headset(struct hsd_info *hi)
{
	int earjack_type;

	HSD_DBG("insert_headset");

	if (hi->set_headset_mic_bias)
		hi->set_headset_mic_bias(1);

	gpio_set_value_cansleep(hi->gpio_mic_en, 1);

	msleep(hi->latency_for_detection);

	earjack_type = gpio_get_value_cansleep(hi->gpio_jpole);

	if (earjack_type == HEADSET_3POLE) {
		HSD_DBG("3 polarity earjack");

		atomic_set(&hi->is_3_pole_or_not, 1);

		mutex_lock(&hi->mutex_lock);
		switch_set_state(&hi->sdev, HEADSET_NO_MIC);
		mutex_unlock(&hi->mutex_lock);

		gpio_set_value_cansleep(hi->gpio_mic_en, 0);
		if (hi->set_headset_mic_bias)
			hi->set_headset_mic_bias(0);
		if (hi->set_uart_console)
			hi->set_uart_console(0);

		input_report_switch(hi->input, SW_HEADPHONE_INSERT, 1);
		input_sync(hi->input);
	} else {
		HSD_DBG("4 polarity earjack");

		atomic_set(&hi->is_3_pole_or_not, 0);

		mutex_lock(&hi->mutex_lock);
		switch_set_state(&hi->sdev, HEADSET_WITH_MIC);
		mutex_unlock(&hi->mutex_lock);

		if (!atomic_read(&hi->irq_key_enabled)) {
			HSD_DBG("enable_irq - irq_key");
			enable_irq(hi->irq_key);

			atomic_set(&hi->irq_key_enabled, 1);
		}
		if (hi->set_uart_console)
			hi->set_uart_console(0);
		input_report_switch(hi->input, SW_HEADPHONE_INSERT, 1);
		input_report_switch(hi->input, SW_MICROPHONE_INSERT, 1);
		input_sync(hi->input);
	}
}

static void remove_headset(struct hsd_info *hi)
{
	int has_mic = switch_get_state(&hi->sdev);

	HSD_DBG("remove_headset");

	gpio_set_value_cansleep(hi->gpio_mic_en, 0);
	if (hi->set_headset_mic_bias)
		hi->set_headset_mic_bias(0);

	atomic_set(&hi->is_3_pole_or_not, 1);
	mutex_lock(&hi->mutex_lock);
	switch_set_state(&hi->sdev, NO_DEVICE);
	mutex_unlock(&hi->mutex_lock);

	if (atomic_read(&hi->irq_key_enabled)) {
		disable_irq(hi->irq_key);
		atomic_set(&hi->irq_key_enabled, 0);
	}

	if (atomic_read(&hi->btn_state))
#ifdef	FSA8008_USE_WORK_QUEUE
	queue_delayed_work(local_fsa8008_workqueue,
			&(hi->work_for_key_released), hi->latency_for_key );
#else
	schedule_delayed_work(&(hi->work_for_key_released),
			hi->latency_for_key );
#endif
	input_report_switch(hi->input, SW_HEADPHONE_INSERT, 0);
	if (has_mic == HEADSET_WITH_MIC)
		input_report_switch(hi->input, SW_MICROPHONE_INSERT, 0);
	input_sync(hi->input);
}

static void detect_work(struct work_struct *work)
{
	int state;
	struct delayed_work *dwork = container_of(
			work, struct delayed_work, work);
	struct hsd_info *hi = container_of(dwork, struct hsd_info, work);

	state = gpio_get_value_cansleep(hi->gpio_detect);

	if (state == HEADSET_REMOVE) {
		if (switch_get_state(&hi->sdev) != NO_DEVICE) {
			remove_headset(hi);
		} else {
			HSD_DBG("err_invalid_state state = %d\n", state);
		}
	} else {

		if (switch_get_state(&hi->sdev) == NO_DEVICE) {
			insert_headset(hi);
		} else {
			HSD_DBG("err_invalid_state state = %d\n", state);
		}
	}

}

static void schedule_detect_work(struct hsd_info *hi)
{
	wake_lock_timeout(&ear_hook_wake_lock, FSA8008_WAKELOCK_TIMEOUT);

#ifdef FSA8008_USE_WORK_QUEUE
	queue_delayed_work(local_fsa8008_workqueue, &(hi->work),
			msecs_to_jiffies(FSA8008_DEBOUNCE_TIME));
#else
	schedule_delayed_work(&(hi->work),
			msecs_to_jiffies(FSA8008_DEBOUNCE_TIME));
#endif
}

static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
{
	struct hsd_info *hi = (struct hsd_info *) dev_id;

	HSD_DBG("gpio_irq_handler");

	schedule_detect_work(hi);

	return IRQ_HANDLED;
}

static irqreturn_t button_irq_handler(int irq, void *dev_id)
{
	struct hsd_info *hi = (struct hsd_info *) dev_id;
	int value;

	HSD_DBG("button_irq_handler");

	wake_lock_timeout(&ear_hook_wake_lock, FSA8008_WAKELOCK_TIMEOUT);

	value = gpio_get_value_cansleep(hi->gpio_key);

#ifdef	FSA8008_USE_WORK_QUEUE
	if (value)
		queue_delayed_work(local_fsa8008_workqueue,
				&(hi->work_for_key_pressed),
				hi->latency_for_key );
	else
		queue_delayed_work(local_fsa8008_workqueue,
				&(hi->work_for_key_released),
				hi->latency_for_key );
#else
	if (value)
		schedule_delayed_work(&(hi->work_for_key_pressed),
				hi->latency_for_key );
	else
		schedule_delayed_work(&(hi->work_for_key_released),
				hi->latency_for_key );
#endif

	return IRQ_HANDLED;
}

static int hsd_gpio_init(struct hsd_info *hi)
{
	int ret;

	/* initialize gpio_detect */
	ret = gpio_request_one(hi->gpio_detect, GPIOF_IN, "gpio_detect");
	if (ret < 0) {
		pr_err("%s: Failed to gpio_request gpio%d (gpio_detect)\n",
				__func__, hi->gpio_detect);
		goto error_01;
	}

	/* initialize gpio_jpole */
	ret = gpio_request_one(hi->gpio_jpole, GPIOF_IN, "gpio_jpole");
	if (ret < 0) {
		pr_err("%s: Failed to gpio_request gpio%d (gpio_jpole)\n",
				__func__, hi->gpio_jpole);
		goto error_02;
	}

	/* initialize gpio_key */
	ret = gpio_request_one(hi->gpio_key, GPIOF_IN, "gpio_key");
	if (ret < 0) {
		pr_err("%s: Failed to gpio_request gpio%d (gpio_key)\n",
				__func__, hi->gpio_key);
		goto error_03;
	}

	/* initialize gpio_mic_en */
	ret = gpio_request_one(hi->gpio_mic_en, GPIOF_OUT_INIT_LOW,
			"gpio_mic_en");
	if (ret < 0) {
		pr_err("%s: Failed to gpio_request gpio%d (gpio_mic_en)\n",
				__func__, hi->gpio_mic_en);
		goto error_04;
	}

	/* initialize gpio_mic_bias_en */
	if (gpio_is_valid(hi->gpio_mic_bias_en)) {
		ret = gpio_request_one(hi->gpio_mic_bias_en,
				GPIOF_OUT_INIT_LOW, "gpio_mic_bias_en");
		if (ret < 0) {
			pr_err("%s: Failed to gpio_request gpio%d "
			       "(gpio_mic_bias_en)\n",
					__func__, hi->gpio_mic_en);
			goto error_05;
		}
	}

	return 0;

error_05:
	gpio_free(hi->gpio_mic_en);
error_04:
	gpio_free(hi->gpio_key);
error_03:
	gpio_free(hi->gpio_jpole);
error_02:
	gpio_free(hi->gpio_detect);
error_01:
	return ret;
}

static void hsd_gpio_free(struct hsd_info *hi)
{
	if (gpio_is_valid(hi->gpio_mic_bias_en))
		gpio_free(hi->gpio_mic_bias_en);
	gpio_free(hi->gpio_mic_en);
	gpio_free(hi->gpio_key);
	gpio_free(hi->gpio_jpole);
	gpio_free(hi->gpio_detect);
}

static int hsd_probe(struct platform_device *pdev)
{
	int ret = 0;
	struct fsa8008_platform_data *pdata = pdev->dev.platform_data;
	struct hsd_info *hi;

	pr_info("fsa8008 probe\n");

	if (!pdata) {
		pr_err("%s: no pdata\n", __func__);
		return -ENODEV;
	}

	hi = kzalloc(sizeof(struct hsd_info), GFP_KERNEL);
	if (NULL == hi) {
		pr_err("%s: out of memory\n", __func__);
		return -ENOMEM;
	}

	hi->key_code = pdata->key_code;

	platform_set_drvdata(pdev, hi);

	atomic_set(&hi->btn_state, 0);
	atomic_set(&hi->is_3_pole_or_not, 1);

	hi->gpio_detect = pdata->gpio_detect;
	hi->gpio_detect_can_wakeup = pdata->gpio_detect_can_wakeup;
	hi->gpio_mic_en = pdata->gpio_mic_en;
	hi->gpio_mic_bias_en = pdata->gpio_mic_bias_en;
	hi->gpio_jpole = pdata->gpio_jpole;
	hi->gpio_key = pdata->gpio_key;
	hi->set_headset_mic_bias = pdata->set_headset_mic_bias;
	hi->set_uart_console = pdata->set_uart_console;
	hi->latency_for_detection = pdata->latency_for_detection;
	hi->latency_for_key = msecs_to_jiffies(FSA8008_KEY_LATENCY_TIME);

	mutex_init(&hi->mutex_lock);

	INIT_DELAYED_WORK(&hi->work, detect_work);
	INIT_DELAYED_WORK(&hi->work_for_key_pressed, button_pressed);
	INIT_DELAYED_WORK(&hi->work_for_key_released, button_released);

	if (hsd_gpio_init(hi) < 0)
		goto error_01;

	/* initialize irq of gpio_jpole */
	hi->irq_detect = gpio_to_irq(hi->gpio_detect);
	HSD_DBG("hi->irq_detect = %d\n", hi->irq_detect);
	if (hi->irq_detect < 0) {
		pr_err("%s: Failed to get interrupt number\n", __func__);
		ret = hi->irq_detect;
		goto error_02;
	}

	ret = request_threaded_irq(hi->irq_detect, NULL, gpio_irq_handler,
			IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING,
			pdev->name, hi);
	if (ret) {
		pr_err("%s: failed to request button irq\n", __func__);
		goto error_02;
	}

	if (hi->gpio_detect_can_wakeup) {
		ret = irq_set_irq_wake(hi->irq_detect, 1);
		if (ret < 0) {
			pr_err("%s: Failed to set irq_detect interrupt wake\n",
					__func__);
			goto error_03;
		}
	}

	/* initialize irq of gpio_key */
	hi->irq_key = gpio_to_irq(hi->gpio_key);
	HSD_DBG("hi->irq_key = %d\n", hi->irq_key);
	if (hi->irq_key < 0) {
		pr_err("%s: Failed to get interrupt number\n", __func__);
		ret = hi->irq_key;
		goto error_03;
	}

	ret = request_threaded_irq(hi->irq_key, NULL, button_irq_handler,
			IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING,
			pdev->name, hi);
	if (ret) {
		pr_err("%s: failed to request button irq\n", __func__);
		goto error_03;
	}

	disable_irq(hi->irq_key);

	ret = irq_set_irq_wake(hi->irq_key, 1);
	if (ret < 0) {
		pr_err("%s: Failed to set irq_key interrupt wake\n", __func__);
		goto error_04;
	}

	/* initialize switch device */
	hi->sdev.name = pdata->switch_name;
	hi->sdev.print_state = hsd_print_state;
	hi->sdev.print_name = hsd_print_name;

	ret = switch_dev_register(&hi->sdev);
	if (ret < 0) {
		pr_err("%s: Failed to register switch device\n", __func__);
		goto error_04;
	}

	/* initialize input device */
	hi->input = input_allocate_device();
	if (!hi->input) {
		pr_err("%s: Failed to allocate input device\n", __func__);
		ret = -ENOMEM;
		goto error_05;
	}

	hi->input->name = pdata->keypad_name;

	hi->input->id.vendor    = 0x0001;
	hi->input->id.product   = 1;
	hi->input->id.version   = 1;

	set_bit(EV_SYN, hi->input->evbit);
	set_bit(EV_KEY, hi->input->evbit);
	set_bit(EV_SW, hi->input->evbit);
	set_bit(hi->key_code, hi->input->keybit);
	set_bit(SW_HEADPHONE_INSERT, hi->input->swbit);
	set_bit(SW_MICROPHONE_INSERT, hi->input->swbit);

	ret = input_register_device(hi->input);
	if (ret) {
		pr_err("%s: Failed to register input device\n", __func__);
		goto error_06;
	}

	if (!gpio_get_value_cansleep(hi->gpio_detect)) {
#ifdef FSA8008_USE_WORK_QUEUE
		/* to detect in initialization with eacjack insertion */
		queue_delayed_work(local_fsa8008_workqueue, &(hi->work), 0);
#else
		/* to detect in initialization with eacjack insertion */
		schedule_delayed_work(&(hi->work), 0);
#endif
	}

	return ret;

error_06:
	input_free_device(hi->input);
error_05:
	switch_dev_unregister(&hi->sdev);
error_04:
	free_irq(hi->irq_key, 0);
error_03:
	free_irq(hi->irq_detect, 0);
error_02:
	hsd_gpio_free(hi);
error_01:
	mutex_destroy(&hi->mutex_lock);
	kfree(hi);

	return ret;
}

static int hsd_remove(struct platform_device *pdev)
{
	struct hsd_info *hi = (struct hsd_info *)platform_get_drvdata(pdev);

	if (switch_get_state(&hi->sdev))
		remove_headset(hi);

	input_unregister_device(hi->input);
	switch_dev_unregister(&hi->sdev);

	free_irq(hi->irq_key, 0);
	free_irq(hi->irq_detect, 0);

	hsd_gpio_free(hi);

	mutex_destroy(&hi->mutex_lock);
	kfree(hi);

	return 0;
}

static int hsd_suspend(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct hsd_info *hi = platform_get_drvdata(pdev);

	if (!hi->gpio_detect_can_wakeup) {
		disable_irq(hi->irq_detect);
		hi->saved_detect = gpio_get_value(hi->gpio_detect);
	}

	return 0;
}

static int hsd_resume(struct device *dev)
{

	struct platform_device *pdev = to_platform_device(dev);
	struct hsd_info *hi = platform_get_drvdata(pdev);
	int detect = 0;

	detect = gpio_get_value(hi->gpio_detect);
	if (HEADSET_INSERT == detect)
		if (hi->set_uart_console)
			hi->set_uart_console(0);

	if (!hi->gpio_detect_can_wakeup) {
		enable_irq(hi->irq_detect);
		if (hi->saved_detect != detect)
			schedule_detect_work(hi);
	}

	return 0;
}

static const struct dev_pm_ops hsd_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(hsd_suspend, hsd_resume)
};

static struct platform_driver hsd_driver = {
	.probe          = hsd_probe,
	.remove         = hsd_remove,
	.driver         = {
		.name   = "fsa8008",
		.owner  = THIS_MODULE,
		.pm = &hsd_pm_ops,
	},
};

static int __init hsd_init(void)
{
	int ret;

	pr_info("fsa8008 init\n");

	wake_lock_init(&ear_hook_wake_lock, WAKE_LOCK_SUSPEND, "ear_hook");

#ifdef FSA8008_USE_WORK_QUEUE
	local_fsa8008_workqueue = create_workqueue("fsa8008");
	if (!local_fsa8008_workqueue) {
		pr_err("%s: out of memory\n", __func__);
		ret = -ENOMEM;
		goto err_workqueue;
	}
#endif

	ret = platform_driver_register(&hsd_driver);
	if (ret < 0) {
		pr_err("%s: Fail to register platform driver\n", __func__);
		goto err_platform_driver_register;
	}

	return 0;

err_platform_driver_register:
#ifdef FSA8008_USE_WORK_QUEUE
	if (local_fsa8008_workqueue)
		destroy_workqueue(local_fsa8008_workqueue);
	local_fsa8008_workqueue = NULL;
#endif
err_workqueue:
	wake_lock_destroy(&ear_hook_wake_lock);

	return ret;
}

static void __exit hsd_exit(void)
{
#ifdef FSA8008_USE_WORK_QUEUE
	if (local_fsa8008_workqueue)
		destroy_workqueue(local_fsa8008_workqueue);
	local_fsa8008_workqueue = NULL;
#endif

	platform_driver_unregister(&hsd_driver);
	wake_lock_destroy(&ear_hook_wake_lock);
}

/* to make init after pmicxxxx module */
late_initcall_sync(hsd_init);
module_exit(hsd_exit);

MODULE_AUTHOR("Yoon Gi Souk <gisouk.yoon@lge.com>");
MODULE_DESCRIPTION("FSA8008 Headset detection driver");
MODULE_LICENSE("GPL");