forked from ccyrowski/cm-kernel
/
htc_headset.c
1421 lines (1228 loc) · 34.2 KB
/
htc_headset.c
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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* H2W device detection driver.
*
* Copyright (C) 2008 Google, Inc.
* Copyright (C) 2008 HTC, Inc.
*
* Authors:
* Laurence Chen <Laurence_Chen@htc.com>
* Nick Pelly <npelly@google.com>
* Thomas Tsai <thomas_tsai@htc.com>
* Farmer Tseng <farmer_tseng@htc.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*/
/* For detecting HTC 2 Wire devices, such as wired headset.
Logically, the H2W driver is always present, and H2W state (hi->state)
indicates what is currently plugged into the H2W interface.
When the headset is plugged in, CABLE_IN1 is pulled low. When the headset
button is pressed, CABLE_IN2 is pulled low. These two lines are shared with
the TX and RX (respectively) of UART3 - used for serial debugging.
This headset driver keeps the CPLD configured as UART3 for as long as
possible, so that we can do serial FIQ debugging even when the kernel is
locked and this driver no longer runs. So it only configures the CPLD to
GPIO while the headset is plugged in, and for 10ms during detection work.
Unfortunately we can't leave the CPLD as UART3 while a headset is plugged
in, UART3 is pullup on TX but the headset is pull-down, causing a 55 mA
drain on trout.
The headset detection work involves setting CPLD to GPIO, and then pulling
CABLE_IN1 high with a stronger pullup than usual. A H2W headset will still
pull this line low, whereas other attachments such as a serial console
would get pulled up by this stronger pullup.
Headset insertion/removal causes UEvent's to be sent, and
/sys/class/switch/h2w/state to be updated.
Button presses are interpreted as input event (KEY_MEDIA). Button presses
are ignored if the headset is plugged in, so the buttons on 11 pin -> 3.5mm
jack adapters do not work until a headset is plugged into the adapter. This
is to avoid serial RX traffic causing spurious button press events.
We tend to check the status of CABLE_IN1 a few more times than strictly
necessary during headset detection, to avoid spurious headset insertion
events caused by serial debugger TX traffic.
*/
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/sysdev.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/workqueue.h>
#include <linux/irq.h>
#include <linux/delay.h>
#include <linux/types.h>
#include <linux/input.h>
#include <linux/platform_device.h>
#include <linux/mutex.h>
#include <linux/errno.h>
#include <linux/err.h>
#include <linux/hrtimer.h>
#include <linux/switch.h>
#include <linux/input.h>
#include <linux/debugfs.h>
#include <asm/gpio.h>
#include <asm/atomic.h>
#include <mach/board.h>
#include <mach/vreg.h>
#include <asm/mach-types.h>
#include <mach/htc_headset.h>
#define H2WI(fmt, arg...) \
printk(KERN_INFO "[H2W] %s " fmt "\r\n", __func__, ## arg)
#define H2WE(fmt, arg...) \
printk(KERN_ERR "[H2W] %s " fmt "\r\n", __func__, ## arg)
#ifdef CONFIG_DEBUG_H2W
#define H2W_DBG(fmt, arg...) printk(KERN_INFO "[H2W] %s " fmt "\r\n", __func__, ## arg)
#else
#define H2W_DBG(fmt, arg...) do {} while (0)
#endif
static struct workqueue_struct *g_detection_work_queue;
static void detection_work(struct work_struct *work);
static DECLARE_WORK(g_detection_work, detection_work);
static void headset35mm_detection_work(struct work_struct *work);
static DECLARE_WORK(g_extend_detection_work, headset35mm_detection_work);
struct h2w_info {
struct switch_dev sdev;
struct input_dev *input;
struct mutex mutex_lock;
atomic_t btn_state;
int ignore_btn;
unsigned int irq;
unsigned int irq_btn;
unsigned int irq_btn_35mm;
int cable_in1;
int cable_in2;
int h2w_clk;
int h2w_data;
int debug_uart;
int headset_mic_35mm;
int ext_mic_sel;
int wfm_ant_sw;
void (*config_cpld) (int);
void (*init_cpld) (void);
/* for h2w */
void (*set_dat)(int);
void (*set_clk)(int);
void (*set_dat_dir)(int);
void (*set_clk_dir)(int);
int (*get_dat)(void);
int (*get_clk)(void);
int htc_headset_flag;
int headset_35mm_flag;
int btn_11pin_35mm_flag;
int headset_35mm_insert;
struct hrtimer timer;
ktime_t debounce_time;
struct hrtimer btn_timer;
ktime_t btn_debounce_time;
struct hrtimer btn35mm_timer;
ktime_t btn35mm_debounce_time;
H2W_INFO h2w_info;
H2W_SPEED speed;
struct vreg *vreg_h2w;
};
static struct h2w_info *hi;
static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
{
switch (switch_get_state(&hi->sdev)) {
case H2W_NO_DEVICE:
return sprintf(buf, "No Device\n");
case H2W_HTC_HEADSET:
return sprintf(buf, "Headset\n");
}
return -EINVAL;
}
static void button_pressed(void)
{
printk(KERN_INFO "[H2W] button_pressed\n");
atomic_set(&hi->btn_state, 1);
input_report_key(hi->input, KEY_MEDIA, 1);
input_sync(hi->input);
}
static void button_released(void)
{
printk(KERN_INFO "[H2W] button_released\n");
atomic_set(&hi->btn_state, 0);
input_report_key(hi->input, KEY_MEDIA, 0);
input_sync(hi->input);
}
/*****************
* H2W proctocol *
*****************/
static inline void h2w_begin_command(void)
{
/* Disable H2W interrupt */
set_irq_type(hi->irq_btn, IRQF_TRIGGER_HIGH);
disable_irq(hi->irq);
disable_irq(hi->irq_btn);
/* Set H2W_CLK as output low */
hi->set_clk(0);
hi->set_clk_dir(1);
}
static inline void h2w_end_command(void)
{
/* Set H2W_CLK as input */
hi->set_clk_dir(0);
/* Enable H2W interrupt */
enable_irq(hi->irq);
enable_irq(hi->irq_btn);
set_irq_type(hi->irq_btn, IRQF_TRIGGER_RISING);
}
static inline void one_clock_write(unsigned short flag)
{
if (flag)
hi->set_dat(1);
else
hi->set_dat(0);
udelay(hi->speed);
hi->set_clk(1);
udelay(hi->speed);
hi->set_clk(0);
}
static inline void one_clock_write_RWbit(unsigned short flag)
{
if (flag)
hi->set_dat(1);
else
hi->set_dat(0);
udelay(hi->speed);
hi->set_clk(1);
udelay(hi->speed);
hi->set_clk(0);
hi->set_dat_dir(0);
udelay(hi->speed);
}
static inline void h2w_reset(void)
{
/* Set H2W_DAT as output low */
hi->set_dat(0);
hi->set_dat_dir(1);
udelay(hi->speed);
hi->set_clk(1);
udelay(4 * hi->speed);
hi->set_dat(1);
udelay(hi->speed);
hi->set_dat(0);
udelay(hi->speed);
hi->set_clk(0);
udelay(hi->speed);
}
static inline void h2w_start(void)
{
udelay(hi->speed);
hi->set_clk(1);
udelay(2 * hi->speed);
hi->set_clk(0);
udelay(hi->speed);
}
static inline int h2w_ack(void)
{
int retry_times = 0;
ack_resend:
if (retry_times == MAX_ACK_RESEND_TIMES)
return -1;
udelay(hi->speed);
hi->set_clk(1);
udelay(2 * hi->speed);
if (!hi->get_dat()) {
retry_times++;
hi->set_clk(0);
udelay(hi->speed);
goto ack_resend;
}
hi->set_clk(0);
udelay(hi->speed);
return 0;
}
static unsigned char h2w_readc(void)
{
unsigned char h2w_read_data = 0x0;
int index;
for (index = 0; index < 8; index++) {
hi->set_clk(0);
udelay(hi->speed);
hi->set_clk(1);
udelay(hi->speed);
if (hi->get_dat())
h2w_read_data |= (1 << (7 - index));
}
hi->set_clk(0);
udelay(hi->speed);
return h2w_read_data;
}
static int h2w_readc_cmd(H2W_ADDR address)
{
int ret = -1, retry_times = 0;
unsigned char read_data;
read_resend:
if (retry_times == MAX_HOST_RESEND_TIMES)
goto err_read;
h2w_reset();
h2w_start();
/* Write address */
one_clock_write(address & 0x1000);
one_clock_write(address & 0x0800);
one_clock_write(address & 0x0400);
one_clock_write(address & 0x0200);
one_clock_write(address & 0x0100);
one_clock_write(address & 0x0080);
one_clock_write(address & 0x0040);
one_clock_write(address & 0x0020);
one_clock_write(address & 0x0010);
one_clock_write(address & 0x0008);
one_clock_write(address & 0x0004);
one_clock_write(address & 0x0002);
one_clock_write(address & 0x0001);
one_clock_write_RWbit(1);
if (h2w_ack() < 0) {
H2W_DBG("Addr NO ACK(%d).\n", retry_times);
retry_times++;
hi->set_clk(0);
mdelay(RESEND_DELAY);
goto read_resend;
}
read_data = h2w_readc();
if (h2w_ack() < 0) {
H2W_DBG("Data NO ACK(%d).\n", retry_times);
retry_times++;
hi->set_clk(0);
mdelay(RESEND_DELAY);
goto read_resend;
}
ret = (int)read_data;
err_read:
if (ret < 0)
H2WE("NO ACK.\n");
return ret;
}
static int h2w_writec_cmd(H2W_ADDR address, unsigned char data)
{
int ret = -1;
int retry_times = 0;
write_resend:
if (retry_times == MAX_HOST_RESEND_TIMES)
goto err_write;
h2w_reset();
h2w_start();
/* Write address */
one_clock_write(address & 0x1000);
one_clock_write(address & 0x0800);
one_clock_write(address & 0x0400);
one_clock_write(address & 0x0200);
one_clock_write(address & 0x0100);
one_clock_write(address & 0x0080);
one_clock_write(address & 0x0040);
one_clock_write(address & 0x0020);
one_clock_write(address & 0x0010);
one_clock_write(address & 0x0008);
one_clock_write(address & 0x0004);
one_clock_write(address & 0x0002);
one_clock_write(address & 0x0001);
one_clock_write_RWbit(0);
if (h2w_ack() < 0) {
H2W_DBG("Addr NO ACK(%d).\n", retry_times);
retry_times++;
hi->set_clk(0);
mdelay(RESEND_DELAY);
goto write_resend;
}
/* Write data */
hi->set_dat_dir(1);
one_clock_write(data & 0x0080);
one_clock_write(data & 0x0040);
one_clock_write(data & 0x0020);
one_clock_write(data & 0x0010);
one_clock_write(data & 0x0008);
one_clock_write(data & 0x0004);
one_clock_write(data & 0x0002);
one_clock_write_RWbit(data & 0x0001);
if (h2w_ack() < 0) {
H2W_DBG("Data NO ACK(%d).\n", retry_times);
retry_times++;
hi->set_clk(0);
mdelay(RESEND_DELAY);
goto write_resend;
}
ret = 0;
err_write:
if (ret < 0)
H2WE("NO ACK.\n");
return ret;
}
static int h2w_get_fnkey(void)
{
int ret;
h2w_begin_command();
ret = h2w_readc_cmd(H2W_FNKEY_UPDOWN);
h2w_end_command();
return ret;
}
static int h2w_dev_init(H2W_INFO *ph2w_info)
{
int ret = -1;
unsigned char ascr0 = 0;
int h2w_sys = 0, maxgpadd = 0, maxadd = 0, key = 0;
hi->speed = H2W_50KHz;
h2w_begin_command();
/* read H2W_SYSTEM */
h2w_sys = h2w_readc_cmd(H2W_SYSTEM);
if (h2w_sys == -1) {
H2WE("read H2W_SYSTEM(0x0000) failed.\n");
goto err_plugin;
}
ph2w_info->ACC_CLASS = (h2w_sys & 0x03);
ph2w_info->AUDIO_DEVICE = (h2w_sys & 0x04) > 0 ? 1 : 0;
ph2w_info->HW_REV = (h2w_sys & 0x18) >> 3;
ph2w_info->SLEEP_PR = (h2w_sys & 0x20) >> 5;
ph2w_info->CLK_SP = (h2w_sys & 0xC0) >> 6;
/* enter init mode */
if (h2w_writec_cmd(H2W_ASCR0, H2W_ASCR_DEVICE_INI) < 0) {
H2WE("write H2W_ASCR0(0x0002) failed.\n");
goto err_plugin;
}
udelay(10);
/* read H2W_MAX_GP_ADD */
maxgpadd = h2w_readc_cmd(H2W_MAX_GP_ADD);
if (maxgpadd == -1) {
H2WE("write H2W_MAX_GP_ADD(0x0001) failed.\n");
goto err_plugin;
}
ph2w_info->CLK_SP += (maxgpadd & 0x60) >> 3;
ph2w_info->MAX_GP_ADD = (maxgpadd & 0x1F);
/* read key group */
if (ph2w_info->MAX_GP_ADD >= 1) {
ph2w_info->KEY_MAXADD = h2w_readc_cmd(H2W_KEY_MAXADD);
if (ph2w_info->KEY_MAXADD == -1)
goto err_plugin;
if (ph2w_info->KEY_MAXADD >= 1) {
key = h2w_readc_cmd(H2W_ASCII_DOWN);
if (key < 0)
goto err_plugin;
ph2w_info->ASCII_DOWN = (key == 0xFF) ? 1 : 0;
}
if (ph2w_info->KEY_MAXADD >= 2) {
key = h2w_readc_cmd(H2W_ASCII_UP);
if (key == -1)
goto err_plugin;
ph2w_info->ASCII_UP = (key == 0xFF) ? 1 : 0;
}
if (ph2w_info->KEY_MAXADD >= 3) {
key = h2w_readc_cmd(H2W_FNKEY_UPDOWN);
if (key == -1)
goto err_plugin;
ph2w_info->FNKEY_UPDOWN = (key == 0xFF) ? 1 : 0;
}
if (ph2w_info->KEY_MAXADD >= 4) {
key = h2w_readc_cmd(H2W_KD_STATUS);
if (key == -1)
goto err_plugin;
ph2w_info->KD_STATUS = (key == 0x01) ? 1 : 0;
}
}
/* read led group */
if (ph2w_info->MAX_GP_ADD >= 2) {
ph2w_info->LED_MAXADD = h2w_readc_cmd(H2W_LED_MAXADD);
if (ph2w_info->LED_MAXADD == -1)
goto err_plugin;
if (ph2w_info->LED_MAXADD >= 1) {
key = h2w_readc_cmd(H2W_LEDCT0);
if (key == -1)
goto err_plugin;
ph2w_info->LEDCT0 = (key == 0x02) ? 1 : 0;
}
}
/* read group 3, 4, 5 */
if (ph2w_info->MAX_GP_ADD >= 3) {
maxadd = h2w_readc_cmd(H2W_CRDL_MAXADD);
if (maxadd == -1)
goto err_plugin;
}
if (ph2w_info->MAX_GP_ADD >= 4) {
maxadd = h2w_readc_cmd(H2W_CARKIT_MAXADD);
if (maxadd == -1)
goto err_plugin;
}
if (ph2w_info->MAX_GP_ADD >= 5) {
maxadd = h2w_readc_cmd(H2W_USBHOST_MAXADD);
if (maxadd == -1)
goto err_plugin;
}
/* read medical group */
if (ph2w_info->MAX_GP_ADD >= 6) {
ph2w_info->MED_MAXADD = h2w_readc_cmd(H2W_MED_MAXADD);
if (ph2w_info->MED_MAXADD == -1)
goto err_plugin;
if (ph2w_info->MED_MAXADD >= 1) {
key = h2w_readc_cmd(H2W_MED_CONTROL);
if (key == -1)
goto err_plugin;
ph2w_info->DATA_EN = (key & 0x01);
ph2w_info->AP_EN = (key & 0x02) >> 1;
ph2w_info->AP_ID = (key & 0x1c) >> 2;
}
if (ph2w_info->MED_MAXADD >= 2) {
key = h2w_readc_cmd(H2W_MED_IN_DATA);
if (key == -1)
goto err_plugin;
}
}
if (ph2w_info->AUDIO_DEVICE)
ascr0 = H2W_ASCR_AUDIO_IN | H2W_ASCR_ACT_EN;
else
ascr0 = H2W_ASCR_ACT_EN;
if (h2w_writec_cmd(H2W_ASCR0, ascr0) < 0)
goto err_plugin;
udelay(10);
ret = 0;
/* adjust speed */
if (ph2w_info->MAX_GP_ADD == 2) {
/* Remote control */
hi->speed = H2W_250KHz;
} else if (ph2w_info->MAX_GP_ADD == 6) {
if (ph2w_info->MED_MAXADD >= 1) {
key = h2w_readc_cmd(H2W_MED_CONTROL);
if (key == -1)
goto err_plugin;
ph2w_info->DATA_EN = (key & 0x01);
ph2w_info->AP_EN = (key & 0x02) >> 1;
ph2w_info->AP_ID = (key & 0x1c) >> 2;
}
}
err_plugin:
h2w_end_command();
return ret;
}
static inline void h2w_dev_power_on(int on)
{
if (!hi->vreg_h2w)
return;
if (on)
vreg_enable(hi->vreg_h2w);
else
vreg_disable(hi->vreg_h2w);
}
static int h2w_dev_detect(void)
{
int ret = -1;
int retry_times;
for (retry_times = 5; retry_times; retry_times--) {
/* Enable H2W Power */
h2w_dev_power_on(1);
msleep(100);
memset(&hi->h2w_info, 0, sizeof(H2W_INFO));
if (h2w_dev_init(&hi->h2w_info) < 0) {
h2w_dev_power_on(0);
msleep(100);
} else if (hi->h2w_info.MAX_GP_ADD == 2) {
ret = 0;
break;
} else {
printk(KERN_INFO "h2w_detect: detect error(%d)\n"
, hi->h2w_info.MAX_GP_ADD);
h2w_dev_power_on(0);
msleep(100);
}
printk(KERN_INFO "h2w_detect(%d)\n"
, hi->h2w_info.MAX_GP_ADD);
}
H2W_DBG("h2w_detect:(%d)\n", retry_times);
return ret;
}
static void remove_headset(void)
{
unsigned long irq_flags;
H2W_DBG("");
mutex_lock(&hi->mutex_lock);
switch_set_state(&hi->sdev, switch_get_state(&hi->sdev) &
~(BIT_HEADSET | BIT_HEADSET_NO_MIC));
mutex_unlock(&hi->mutex_lock);
hi->init_cpld();
/* Disable button */
switch (hi->htc_headset_flag) {
case H2W_HTC_HEADSET:
local_irq_save(irq_flags);
disable_irq(hi->irq_btn);
local_irq_restore(irq_flags);
if (atomic_read(&hi->btn_state))
button_released();
printk(KERN_INFO "remove htc headset\n");
break;
case NORMAL_HEARPHONE:
if (hi->btn_11pin_35mm_flag) {
disable_irq(hi->irq_btn_35mm);
turn_mic_bias_on(0);
hi->btn_11pin_35mm_flag = 0;
if (atomic_read(&hi->btn_state))
button_released();
}
printk(KERN_INFO "remove 11pin 3.5mm headset\n");
break;
case H2W_DEVICE:
h2w_dev_power_on(0);
set_irq_type(hi->irq_btn, IRQF_TRIGGER_LOW);
disable_irq(hi->irq_btn);
/* 10ms (5-15 with 10ms tick) */
hi->btn_debounce_time = ktime_set(0, 10000000);
hi->set_clk_dir(0);
hi->set_dat_dir(0);
printk(KERN_INFO "remove h2w device\n");
break;
}
hi->htc_headset_flag = 0;
hi->debounce_time = ktime_set(0, 100000000); /* 100 ms */
}
#ifdef CONFIG_MSM_SERIAL_DEBUGGER
extern void msm_serial_debug_enable(int);
#endif
static void insert_headset(int type)
{
unsigned long irq_flags;
int state;
H2W_DBG("");
hi->htc_headset_flag = type;
state = BIT_HEADSET | BIT_HEADSET_NO_MIC;
state = switch_get_state(&hi->sdev);
state &= ~(BIT_HEADSET_NO_MIC | BIT_HEADSET);
switch (type) {
case H2W_HTC_HEADSET:
printk(KERN_INFO "insert_headset H2W_HTC_HEADSET\n");
state |= BIT_HEADSET;
hi->ignore_btn = !gpio_get_value(hi->cable_in2);
/* Enable button irq */
local_irq_save(irq_flags);
enable_irq(hi->irq_btn);
local_irq_restore(irq_flags);
hi->debounce_time = ktime_set(0, 200000000); /* 20 ms */
break;
case NORMAL_HEARPHONE:
if (hi->headset_mic_35mm) {
/* support 3.5mm earphone with mic */
printk(KERN_INFO "11pin_3.5mm_headset plug in\n");
/* Turn On Mic Bias */
turn_mic_bias_on(1);
/* Wait pin be stable */
msleep(200);
/* Detect headset with or without microphone */
if (gpio_get_value(hi->headset_mic_35mm)) {
/* without microphone */
turn_mic_bias_on(0);
state |= BIT_HEADSET_NO_MIC;
printk(KERN_INFO
"11pin_3.5mm without microphone\n");
} else { /* with microphone */
state |= BIT_HEADSET;
/* Enable button irq */
if (!hi->btn_11pin_35mm_flag) {
set_irq_type(hi->irq_btn_35mm,
IRQF_TRIGGER_HIGH);
enable_irq(hi->irq_btn_35mm);
hi->btn_11pin_35mm_flag = 1;
}
printk(KERN_INFO
"11pin_3.5mm with microphone\n");
}
} else /* not support 3.5mm earphone with mic */
state |= BIT_HEADSET_NO_MIC;
hi->debounce_time = ktime_set(0, 500000000); /* 500 ms */
break;
case H2W_DEVICE:
printk(KERN_INFO "insert_headset H2W_DEVICE\n");
if (!hi->set_dat) {
printk(KERN_INFO "Don't support H2W_DEVICE\n");
hi->htc_headset_flag = 0;
return;
}
if (h2w_dev_detect() < 0) {
printk(KERN_INFO "H2W_DEVICE -- Non detect\n");
remove_headset();
} else {
printk(KERN_INFO "H2W_DEVICE -- detect\n");
hi->btn_debounce_time = ktime_set(0, 0);
local_irq_save(irq_flags);
enable_irq(hi->irq_btn);
set_irq_type(hi->irq_btn, IRQF_TRIGGER_RISING);
local_irq_restore(irq_flags);
state |= BIT_HEADSET;
}
break;
case H2W_USB_CRADLE:
printk(KERN_INFO "insert_headset USB_CRADLE\n");
state |= BIT_HEADSET_NO_MIC;
break;
case H2W_UART_DEBUG:
printk(KERN_INFO "switch to H2W_UART_DEBUG\n");
hi->config_cpld(hi->debug_uart);
default:
return;
}
mutex_lock(&hi->mutex_lock);
switch_set_state(&hi->sdev, state);
mutex_unlock(&hi->mutex_lock);
#ifdef CONFIG_MSM_SERIAL_DEBUGGER
msm_serial_debug_enable(false);
#endif
}
#if 0
static void remove_headset(void)
{
unsigned long irq_flags;
H2W_DBG("");
switch_set_state(&hi->sdev, H2W_NO_DEVICE);
hi->init_cpld();
/* Disable button */
local_irq_save(irq_flags);
disable_irq(hi->irq_btn);
local_irq_restore(irq_flags);
if (atomic_read(&hi->btn_state))
button_released();
hi->debounce_time = ktime_set(0, 100000000); /* 100 ms */
}
#endif
static int is_accessary_pluged_in(void)
{
int type = 0;
int clk1 = 0, dat1 = 0, clk2 = 0, dat2 = 0, clk3 = 0, dat3 = 0;
/* Step1: save H2W_CLK and H2W_DAT */
/* Delay 10ms for pin stable. */
msleep(10);
clk1 = gpio_get_value(hi->h2w_clk);
dat1 = gpio_get_value(hi->h2w_data);
/*
* Step2: set GPIO_CABLE_IN1 as output high and GPIO_CABLE_IN2 as
* input
*/
gpio_direction_output(hi->cable_in1, 1);
gpio_direction_input(hi->cable_in2);
/* Delay 10ms for pin stable. */
msleep(10);
/* Step 3: save H2W_CLK and H2W_DAT */
clk2 = gpio_get_value(hi->h2w_clk);
dat2 = gpio_get_value(hi->h2w_data);
/*
* Step 4: set GPIO_CABLE_IN1 as input and GPIO_CABLE_IN2 as output
* high
*/
gpio_direction_input(hi->cable_in1);
gpio_direction_output(hi->cable_in2, 1);
/* Delay 10ms for pin stable. */
msleep(10);
/* Step 5: save H2W_CLK and H2W_DAT */
clk3 = gpio_get_value(hi->h2w_clk);
dat3 = gpio_get_value(hi->h2w_data);
/* Step 6: set both GPIO_CABLE_IN1 and GPIO_CABLE_IN2 as input */
gpio_direction_input(hi->cable_in1);
gpio_direction_input(hi->cable_in2);
H2WI("(%d,%d) (%d,%d) (%d,%d)",
clk1, dat1, clk2, dat2, clk3, dat3);
if ((clk1 == 0) && (dat1 == 1) &&
(clk2 == 0) && (dat2 == 1) &&
(clk3 == 0) && (dat3 == 1))
type = H2W_HTC_HEADSET;
else if ((clk1 == 0) && (dat1 == 0) &&
(clk2 == 0) && (dat2 == 0) &&
(clk3 == 0) && (dat3 == 0))
type = NORMAL_HEARPHONE;
else if ((clk1 == 0) && (dat1 == 0) &&
(clk2 == 1) && (dat2 == 0) &&
(clk3 == 0) && (dat3 == 1))
type = H2W_DEVICE;
else if ((clk1 == 0) && (dat1 == 0) &&
(clk2 == 1) && (dat2 == 1) &&
(clk3 == 1) && (dat3 == 1))
type = H2W_USB_CRADLE;
else if ((clk1 == 0) && (dat1 == 1) &&
(clk2 == 1) && (dat2 == 1) &&
(clk3 == 0) && (dat3 == 1))
type = H2W_UART_DEBUG;
else
type = H2W_NO_DEVICE;
return type;
}
static void detection_work(struct work_struct *work)
{
unsigned long irq_flags;
int type;
H2W_DBG("");
if (gpio_get_value(hi->cable_in1) != 0) {
/* Headset not plugged in */
if (switch_get_state(&hi->sdev) != H2W_NO_DEVICE)
remove_headset();
return;
}
/* Something plugged in, lets make sure its a headset */
/* Switch CPLD to GPIO to do detection */
hi->config_cpld(H2W_GPIO);
/* Disable headset interrupt while detecting.*/
local_irq_save(irq_flags);
disable_irq(hi->irq);
local_irq_restore(irq_flags);
/* Something plugged in, lets make sure its a headset */
type = is_accessary_pluged_in();
/* Restore IRQs */
local_irq_save(irq_flags);
enable_irq(hi->irq);
local_irq_restore(irq_flags);
insert_headset(type);
}
void headset_button_event(int is_press)
{
if (!is_press) {
if (hi->ignore_btn)
hi->ignore_btn = 0;
else if (atomic_read(&hi->btn_state))
button_released();
} else {
if (!hi->ignore_btn && !atomic_read(&hi->btn_state))
button_pressed();
}
}
static enum hrtimer_restart button_35mm_event_timer_func(struct hrtimer *data)
{
if (gpio_get_value(hi->headset_mic_35mm)) {
headset_button_event(1);
/* 10 ms */
hi->btn35mm_debounce_time = ktime_set(0, 10000000);
} else {
headset_button_event(0);
/* 100 ms */
hi->btn35mm_debounce_time = ktime_set(0, 100000000);
}
return HRTIMER_NORESTART;
}
static enum hrtimer_restart button_event_timer_func(struct hrtimer *data)
{
int key, press, keyname, h2w_key = 1;
H2W_DBG("");
if (switch_get_state(&hi->sdev) & BIT_HEADSET) {
switch (hi->htc_headset_flag) {
case H2W_HTC_HEADSET:
if (!gpio_get_value(hi->cable_in2))
headset_button_event(1); /* press */
else
headset_button_event(0);
break;
case H2W_DEVICE:
if ((hi->get_dat() == 1) && (hi->get_clk() == 1)) {
/* Don't do anything because H2W pull out. */
H2WE("Remote Control pull out.\n");
} else {
key = h2w_get_fnkey();
press = (key > 0x7F) ? 0 : 1;
keyname = key & 0x7F;
/* H2WI("key = %d, press = %d,
keyname = %d \n",
key, press, keyname); */
switch (keyname) {
case H2W_KEY_PLAY:
H2WI("H2W_KEY_PLAY");
key = KEY_PLAYPAUSE;
break;
case H2W_KEY_FORWARD:
H2WI("H2W_KEY_FORWARD");
key = KEY_NEXTSONG;
break;
case H2W_KEY_BACKWARD:
H2WI("H2W_KEY_BACKWARD");
key = KEY_PREVIOUSSONG;
break;
case H2W_KEY_VOLUP:
H2WI("H2W_KEY_VOLUP");
key = KEY_VOLUMEUP;
break;
case H2W_KEY_VOLDOWN:
H2WI("H2W_KEY_VOLDOWN");
key = KEY_VOLUMEDOWN;
break;
case H2W_KEY_PICKUP:
H2WI("H2W_KEY_PICKUP");
key = KEY_SEND;
break;
case H2W_KEY_HANGUP:
H2WI("H2W_KEY_HANGUP");
key = KEY_END;
break;
case H2W_KEY_MUTE:
H2WI("H2W_KEY_MUTE");
key = KEY_MUTE;
break;
case H2W_KEY_HOLD:
H2WI("H2W_KEY_HOLD");
break;
default:
H2WI("default");
h2w_key = 0;
}
if (h2w_key) {
if (press)
H2WI("Press\n");
else
H2WI("Release\n");
input_report_key(hi->input, key, press);
}
}
break;
} /* end switch */
}
return HRTIMER_NORESTART;
}
static enum hrtimer_restart detect_event_timer_func(struct hrtimer *data)
{
H2W_DBG("");
queue_work(g_detection_work_queue, &g_detection_work);
return HRTIMER_NORESTART;
}