blob: a69a2e1ca5f8b81727006b6aec1eaa2bf0f69e34 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
2 * H2W device detection driver.
3 *
4 * Copyright (C) 2008 Google, Inc.
5 * Copyright (C) 2008 HTC, Inc.
6 *
7 * Authors:
8 * Laurence Chen <Laurence_Chen@htc.com>
9 * Nick Pelly <npelly@google.com>
10 * Thomas Tsai <thomas_tsai@htc.com>
11 * Farmer Tseng <farmer_tseng@htc.com>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; version 2 of the License.
16 */
17
18/* For detecting HTC 2 Wire devices, such as wired headset.
19
20 Logically, the H2W driver is always present, and H2W state (hi->state)
21 indicates what is currently plugged into the H2W interface.
22
23 When the headset is plugged in, CABLE_IN1 is pulled low. When the headset
24 button is pressed, CABLE_IN2 is pulled low. These two lines are shared with
25 the TX and RX (respectively) of UART3 - used for serial debugging.
26
27 This headset driver keeps the CPLD configured as UART3 for as long as
28 possible, so that we can do serial FIQ debugging even when the kernel is
29 locked and this driver no longer runs. So it only configures the CPLD to
30 GPIO while the headset is plugged in, and for 10ms during detection work.
31
32 Unfortunately we can't leave the CPLD as UART3 while a headset is plugged
33 in, UART3 is pullup on TX but the headset is pull-down, causing a 55 mA
34 drain on trout.
35
36 The headset detection work involves setting CPLD to GPIO, and then pulling
37 CABLE_IN1 high with a stronger pullup than usual. A H2W headset will still
38 pull this line low, whereas other attachments such as a serial console
39 would get pulled up by this stronger pullup.
40
41 Headset insertion/removal causes UEvent's to be sent, and
42 /sys/class/switch/h2w/state to be updated.
43
44 Button presses are interpreted as input event (KEY_MEDIA). Button presses
45 are ignored if the headset is plugged in, so the buttons on 11 pin -> 3.5mm
46 jack adapters do not work until a headset is plugged into the adapter. This
47 is to avoid serial RX traffic causing spurious button press events.
48
49 We tend to check the status of CABLE_IN1 a few more times than strictly
50 necessary during headset detection, to avoid spurious headset insertion
51 events caused by serial debugger TX traffic.
52*/
53
54#include <linux/module.h>
55#include <linux/sysdev.h>
56#include <linux/fs.h>
57#include <linux/interrupt.h>
58#include <linux/workqueue.h>
59#include <linux/irq.h>
60#include <linux/delay.h>
61#include <linux/types.h>
62#include <linux/input.h>
63#include <linux/platform_device.h>
64#include <linux/mutex.h>
65#include <linux/errno.h>
66#include <linux/err.h>
67#include <linux/hrtimer.h>
68#include <linux/switch.h>
69#include <linux/input.h>
70#include <linux/debugfs.h>
71#include <asm/gpio.h>
72#include <asm/atomic.h>
73#include <mach/board.h>
74#include <mach/vreg.h>
75#include <asm/mach-types.h>
76
77#include <mach/htc_headset.h>
78
79#define H2WI(fmt, arg...) \
80 printk(KERN_INFO "[H2W] %s " fmt "\r\n", __func__, ## arg)
81#define H2WE(fmt, arg...) \
82 printk(KERN_ERR "[H2W] %s " fmt "\r\n", __func__, ## arg)
83
84#ifdef CONFIG_DEBUG_H2W
85#define H2W_DBG(fmt, arg...) printk(KERN_INFO "[H2W] %s " fmt "\r\n", __func__, ## arg)
86#else
87#define H2W_DBG(fmt, arg...) do {} while (0)
88#endif
89
90static struct workqueue_struct *g_detection_work_queue;
91static void detection_work(struct work_struct *work);
92static DECLARE_WORK(g_detection_work, detection_work);
93
94struct h2w_info {
95 struct switch_dev sdev;
96 struct input_dev *input;
97 struct mutex mutex_lock;
98
99 atomic_t btn_state;
100 int ignore_btn;
101
102 unsigned int irq;
103 unsigned int irq_btn;
104
105 int cable_in1;
106 int cable_in2;
107 int h2w_clk;
108 int h2w_data;
109 int debug_uart;
110
111 void (*config_cpld) (int);
112 void (*init_cpld) (void);
113 /* for h2w */
114 void (*set_dat)(int);
115 void (*set_clk)(int);
116 void (*set_dat_dir)(int);
117 void (*set_clk_dir)(int);
118 int (*get_dat)(void);
119 int (*get_clk)(void);
120
121 int htc_headset_flag;
122
123 struct hrtimer timer;
124 ktime_t debounce_time;
125
126 struct hrtimer btn_timer;
127 ktime_t btn_debounce_time;
128
129 H2W_INFO h2w_info;
130 H2W_SPEED speed;
131 struct vreg *vreg_h2w;
132};
133static struct h2w_info *hi;
134
135static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
136{
137 switch (switch_get_state(&hi->sdev)) {
138 case H2W_NO_DEVICE:
139 return sprintf(buf, "No Device\n");
140 case H2W_HTC_HEADSET:
141 return sprintf(buf, "Headset\n");
142 }
143 return -EINVAL;
144}
145
146static void button_pressed(void)
147{
148 H2W_DBG("button_pressed \n");
149 atomic_set(&hi->btn_state, 1);
150 input_report_key(hi->input, KEY_MEDIA, 1);
151 input_sync(hi->input);
152}
153
154static void button_released(void)
155{
156 H2W_DBG("button_released \n");
157 atomic_set(&hi->btn_state, 0);
158 input_report_key(hi->input, KEY_MEDIA, 0);
159 input_sync(hi->input);
160}
161
162/*****************
163 * H2W proctocol *
164 *****************/
165static inline void h2w_begin_command(void)
166{
167 /* Disable H2W interrupt */
168 set_irq_type(hi->irq_btn, IRQF_TRIGGER_HIGH);
169 disable_irq(hi->irq);
170 disable_irq(hi->irq_btn);
171
172 /* Set H2W_CLK as output low */
173 hi->set_clk(0);
174 hi->set_clk_dir(1);
175}
176
177static inline void h2w_end_command(void)
178{
179 /* Set H2W_CLK as input */
180 hi->set_clk_dir(0);
181
182 /* Enable H2W interrupt */
183 enable_irq(hi->irq);
184 enable_irq(hi->irq_btn);
185 set_irq_type(hi->irq_btn, IRQF_TRIGGER_RISING);
186}
187
188/*
189 * One bit write data
190 * ________
191 * SCLK O ______| |______O(L)
192 *
193 *
194 * SDAT I <XXXXXXXXXXXXXXXXXXXX>
195 */
196static inline void one_clock_write(unsigned short flag)
197{
198 if (flag)
199 hi->set_dat(1);
200 else
201 hi->set_dat(0);
202
203 udelay(hi->speed);
204 hi->set_clk(1);
205 udelay(hi->speed);
206 hi->set_clk(0);
207}
208
209/*
210 * One bit write data R/W bit
211 * ________
212 * SCLK ______| |______O(L)
213 * 1----> 1----->
214 * 2-------> ______
215 * SDAT <XXXXXXXXXXXXXX> I
216 * O(H/L)
217 */
218static inline void one_clock_write_RWbit(unsigned short flag)
219{
220 if (flag)
221 hi->set_dat(1);
222 else
223 hi->set_dat(0);
224
225 udelay(hi->speed);
226 hi->set_clk(1);
227 udelay(hi->speed);
228 hi->set_clk(0);
229 hi->set_dat_dir(0);
230 udelay(hi->speed);
231}
232
233/*
234 * H2W Reset
235 * ___________
236 * SCLK O(L)______| |___O(L)
237 * 1---->
238 * 4-->1-->1-->1us-->
239 * ____
240 * SDAT O(L)________ | |_______O(L)
241 *
242 * H2w reset command needs to be issued before every access
243 */
244static inline void h2w_reset(void)
245{
246 /* Set H2W_DAT as output low */
247 hi->set_dat(0);
248 hi->set_dat_dir(1);
249
250 udelay(hi->speed);
251 hi->set_clk(1);
252 udelay(4 * hi->speed);
253 hi->set_dat(1);
254 udelay(hi->speed);
255 hi->set_dat(0);
256 udelay(hi->speed);
257 hi->set_clk(0);
258 udelay(hi->speed);
259}
260
261/*
262 * H2W Start
263 * ___________
264 * SCLK O(L)______| |___O(L)
265 * 1---->
266 * 2----------->1-->
267 *
268 * SDAT O(L)______________________O(L)
269 */
270static inline void h2w_start(void)
271{
272 udelay(hi->speed);
273 hi->set_clk(1);
274 udelay(2 * hi->speed);
275 hi->set_clk(0);
276 udelay(hi->speed);
277}
278
279/*
280 * H2W Ack
281 * __________
282 * SCLK _____| |_______O(L)
283 * 1----> 1------>
284 * 2--------->
285 * ________________________
286 * SDAT become Input mode here I
287 */
288static inline int h2w_ack(void)
289{
290 int retry_times = 0;
291
292ack_resend:
293 if (retry_times == MAX_ACK_RESEND_TIMES)
294 return -1;
295
296 udelay(hi->speed);
297 hi->set_clk(1);
298 udelay(2 * hi->speed);
299
300 if (!hi->get_dat()) {
301 retry_times++;
302 hi->set_clk(0);
303 udelay(hi->speed);
304 goto ack_resend;
305 }
306
307 hi->set_clk(0);
308 udelay(hi->speed);
309 return 0;
310}
311
312/*
313 * One bit read data
314 * ________
315 * SCLK ______| |______O(L)
316 * 2----> 2----->
317 * 2------->
318 * SDAT <XXXXXXXXXXXXXXXXXXXX>I
319 */
320static unsigned char h2w_readc(void)
321{
322 unsigned char h2w_read_data = 0x0;
323 int index;
324
325 for (index = 0; index < 8; index++) {
326 hi->set_clk(0);
327 udelay(hi->speed);
328 hi->set_clk(1);
329 udelay(hi->speed);
330 if (hi->get_dat())
331 h2w_read_data |= (1 << (7 - index));
332 }
333 hi->set_clk(0);
334 udelay(hi->speed);
335
336 return h2w_read_data;
337}
338
339static int h2w_readc_cmd(H2W_ADDR address)
340{
341 int ret = -1, retry_times = 0;
342 unsigned char read_data;
343
344read_resend:
345 if (retry_times == MAX_HOST_RESEND_TIMES)
346 goto err_read;
347
348 h2w_reset();
349 h2w_start();
350 /* Write address */
351 one_clock_write(address & 0x1000);
352 one_clock_write(address & 0x0800);
353 one_clock_write(address & 0x0400);
354 one_clock_write(address & 0x0200);
355 one_clock_write(address & 0x0100);
356 one_clock_write(address & 0x0080);
357 one_clock_write(address & 0x0040);
358 one_clock_write(address & 0x0020);
359 one_clock_write(address & 0x0010);
360 one_clock_write(address & 0x0008);
361 one_clock_write(address & 0x0004);
362 one_clock_write(address & 0x0002);
363 one_clock_write(address & 0x0001);
364 one_clock_write_RWbit(1);
365 if (h2w_ack() < 0) {
366 H2W_DBG("Addr NO ACK(%d).\n", retry_times);
367 retry_times++;
368 hi->set_clk(0);
369 mdelay(RESEND_DELAY);
370 goto read_resend;
371 }
372
373 read_data = h2w_readc();
374
375 if (h2w_ack() < 0) {
376 H2W_DBG("Data NO ACK(%d).\n", retry_times);
377 retry_times++;
378 hi->set_clk(0);
379 mdelay(RESEND_DELAY);
380 goto read_resend;
381 }
382 ret = (int)read_data;
383
384err_read:
385 if (ret < 0)
386 H2WE("NO ACK.\n");
387
388 return ret;
389}
390
391static int h2w_writec_cmd(H2W_ADDR address, unsigned char data)
392{
393 int ret = -1;
394 int retry_times = 0;
395
396write_resend:
397 if (retry_times == MAX_HOST_RESEND_TIMES)
398 goto err_write;
399
400 h2w_reset();
401 h2w_start();
402
403 /* Write address */
404 one_clock_write(address & 0x1000);
405 one_clock_write(address & 0x0800);
406 one_clock_write(address & 0x0400);
407 one_clock_write(address & 0x0200);
408 one_clock_write(address & 0x0100);
409 one_clock_write(address & 0x0080);
410 one_clock_write(address & 0x0040);
411 one_clock_write(address & 0x0020);
412 one_clock_write(address & 0x0010);
413 one_clock_write(address & 0x0008);
414 one_clock_write(address & 0x0004);
415 one_clock_write(address & 0x0002);
416 one_clock_write(address & 0x0001);
417 one_clock_write_RWbit(0);
418 if (h2w_ack() < 0) {
419 H2W_DBG("Addr NO ACK(%d).\n", retry_times);
420 retry_times++;
421 hi->set_clk(0);
422 mdelay(RESEND_DELAY);
423 goto write_resend;
424 }
425
426 /* Write data */
427 hi->set_dat_dir(1);
428 one_clock_write(data & 0x0080);
429 one_clock_write(data & 0x0040);
430 one_clock_write(data & 0x0020);
431 one_clock_write(data & 0x0010);
432 one_clock_write(data & 0x0008);
433 one_clock_write(data & 0x0004);
434 one_clock_write(data & 0x0002);
435 one_clock_write_RWbit(data & 0x0001);
436 if (h2w_ack() < 0) {
437 H2W_DBG("Data NO ACK(%d).\n", retry_times);
438 retry_times++;
439 hi->set_clk(0);
440 mdelay(RESEND_DELAY);
441 goto write_resend;
442 }
443 ret = 0;
444
445err_write:
446 if (ret < 0)
447 H2WE("NO ACK.\n");
448
449 return ret;
450}
451
452static int h2w_get_fnkey(void)
453{
454 int ret;
455 h2w_begin_command();
456 ret = h2w_readc_cmd(H2W_FNKEY_UPDOWN);
457 h2w_end_command();
458 return ret;
459}
460
461static int h2w_dev_init(H2W_INFO *ph2w_info)
462{
463 int ret = -1;
464 unsigned char ascr0 = 0;
465 int h2w_sys = 0, maxgpadd = 0, maxadd = 0, key = 0;
466
467 hi->speed = H2W_50KHz;
468 h2w_begin_command();
469
470 /* read H2W_SYSTEM */
471 h2w_sys = h2w_readc_cmd(H2W_SYSTEM);
472 if (h2w_sys == -1) {
473 H2WE("read H2W_SYSTEM(0x0000) failed.\n");
474 goto err_plugin;
475 }
476 ph2w_info->ACC_CLASS = (h2w_sys & 0x03);
477 ph2w_info->AUDIO_DEVICE = (h2w_sys & 0x04) > 0 ? 1 : 0;
478 ph2w_info->HW_REV = (h2w_sys & 0x18) >> 3;
479 ph2w_info->SLEEP_PR = (h2w_sys & 0x20) >> 5;
480 ph2w_info->CLK_SP = (h2w_sys & 0xC0) >> 6;
481
482 /* enter init mode */
483 if (h2w_writec_cmd(H2W_ASCR0, H2W_ASCR_DEVICE_INI) < 0) {
484 H2WE("write H2W_ASCR0(0x0002) failed.\n");
485 goto err_plugin;
486 }
487 udelay(10);
488
489 /* read H2W_MAX_GP_ADD */
490 maxgpadd = h2w_readc_cmd(H2W_MAX_GP_ADD);
491 if (maxgpadd == -1) {
492 H2WE("write H2W_MAX_GP_ADD(0x0001) failed.\n");
493 goto err_plugin;
494 }
495 ph2w_info->CLK_SP += (maxgpadd & 0x60) >> 3;
496 ph2w_info->MAX_GP_ADD = (maxgpadd & 0x1F);
497
498 /* read key group */
499 if (ph2w_info->MAX_GP_ADD >= 1) {
500 ph2w_info->KEY_MAXADD = h2w_readc_cmd(H2W_KEY_MAXADD);
501 if (ph2w_info->KEY_MAXADD == -1)
502 goto err_plugin;
503 if (ph2w_info->KEY_MAXADD >= 1) {
504 key = h2w_readc_cmd(H2W_ASCII_DOWN);
505 if (key < 0)
506 goto err_plugin;
507 ph2w_info->ASCII_DOWN = (key == 0xFF) ? 1 : 0;
508 }
509 if (ph2w_info->KEY_MAXADD >= 2) {
510 key = h2w_readc_cmd(H2W_ASCII_UP);
511 if (key == -1)
512 goto err_plugin;
513 ph2w_info->ASCII_UP = (key == 0xFF) ? 1 : 0;
514 }
515 if (ph2w_info->KEY_MAXADD >= 3) {
516 key = h2w_readc_cmd(H2W_FNKEY_UPDOWN);
517 if (key == -1)
518 goto err_plugin;
519 ph2w_info->FNKEY_UPDOWN = (key == 0xFF) ? 1 : 0;
520 }
521 if (ph2w_info->KEY_MAXADD >= 4) {
522 key = h2w_readc_cmd(H2W_KD_STATUS);
523 if (key == -1)
524 goto err_plugin;
525 ph2w_info->KD_STATUS = (key == 0x01) ? 1 : 0;
526 }
527 }
528
529 /* read led group */
530 if (ph2w_info->MAX_GP_ADD >= 2) {
531 ph2w_info->LED_MAXADD = h2w_readc_cmd(H2W_LED_MAXADD);
532 if (ph2w_info->LED_MAXADD == -1)
533 goto err_plugin;
534 if (ph2w_info->LED_MAXADD >= 1) {
535 key = h2w_readc_cmd(H2W_LEDCT0);
536 if (key == -1)
537 goto err_plugin;
538 ph2w_info->LEDCT0 = (key == 0x02) ? 1 : 0;
539 }
540 }
541
542 /* read group 3, 4, 5 */
543 if (ph2w_info->MAX_GP_ADD >= 3) {
544 maxadd = h2w_readc_cmd(H2W_CRDL_MAXADD);
545 if (maxadd == -1)
546 goto err_plugin;
547 }
548 if (ph2w_info->MAX_GP_ADD >= 4) {
549 maxadd = h2w_readc_cmd(H2W_CARKIT_MAXADD);
550 if (maxadd == -1)
551 goto err_plugin;
552 }
553 if (ph2w_info->MAX_GP_ADD >= 5) {
554 maxadd = h2w_readc_cmd(H2W_USBHOST_MAXADD);
555 if (maxadd == -1)
556 goto err_plugin;
557 }
558
559 /* read medical group */
560 if (ph2w_info->MAX_GP_ADD >= 6) {
561 ph2w_info->MED_MAXADD = h2w_readc_cmd(H2W_MED_MAXADD);
562 if (ph2w_info->MED_MAXADD == -1)
563 goto err_plugin;
564 if (ph2w_info->MED_MAXADD >= 1) {
565 key = h2w_readc_cmd(H2W_MED_CONTROL);
566 if (key == -1)
567 goto err_plugin;
568 ph2w_info->DATA_EN = (key & 0x01);
569 ph2w_info->AP_EN = (key & 0x02) >> 1;
570 ph2w_info->AP_ID = (key & 0x1c) >> 2;
571 }
572 if (ph2w_info->MED_MAXADD >= 2) {
573 key = h2w_readc_cmd(H2W_MED_IN_DATA);
574 if (key == -1)
575 goto err_plugin;
576 }
577 }
578
579 if (ph2w_info->AUDIO_DEVICE)
580 ascr0 = H2W_ASCR_AUDIO_IN | H2W_ASCR_ACT_EN;
581 else
582 ascr0 = H2W_ASCR_ACT_EN;
583
584 if (h2w_writec_cmd(H2W_ASCR0, ascr0) < 0)
585 goto err_plugin;
586 udelay(10);
587
588 ret = 0;
589
590 /* adjust speed */
591 if (ph2w_info->MAX_GP_ADD == 2) {
592 /* Remote control */
593 hi->speed = H2W_250KHz;
594 } else if (ph2w_info->MAX_GP_ADD == 6) {
595 if (ph2w_info->MED_MAXADD >= 1) {
596 key = h2w_readc_cmd(H2W_MED_CONTROL);
597 if (key == -1)
598 goto err_plugin;
599 ph2w_info->DATA_EN = (key & 0x01);
600 ph2w_info->AP_EN = (key & 0x02) >> 1;
601 ph2w_info->AP_ID = (key & 0x1c) >> 2;
602 }
603 }
604
605err_plugin:
606 h2w_end_command();
607
608 return ret;
609}
610
611static inline void h2w_dev_power_on(int on)
612{
613 if (!hi->vreg_h2w)
614 return;
615
616 if (on)
617 vreg_enable(hi->vreg_h2w);
618 else
619 vreg_disable(hi->vreg_h2w);
620}
621
622static int h2w_dev_detect(void)
623{
624 int ret = -1;
625 int retry_times;
626
627 for (retry_times = 5; retry_times; retry_times--) {
628 /* Enable H2W Power */
629 h2w_dev_power_on(1);
630 msleep(100);
631 memset(&hi->h2w_info, 0, sizeof(H2W_INFO));
632 if (h2w_dev_init(&hi->h2w_info) < 0) {
633 h2w_dev_power_on(0);
634 msleep(100);
635 } else if (hi->h2w_info.MAX_GP_ADD == 2) {
636 ret = 0;
637 break;
638 } else {
639 printk(KERN_INFO "h2w_detect: detect error(%d)\n"
640 , hi->h2w_info.MAX_GP_ADD);
641 h2w_dev_power_on(0);
642 msleep(100);
643 }
644 printk(KERN_INFO "h2w_detect(%d)\n"
645 , hi->h2w_info.MAX_GP_ADD);
646 }
647 H2W_DBG("h2w_detect:(%d)\n", retry_times);
648 return ret;
649}
650
651static void remove_headset(void)
652{
653 unsigned long irq_flags;
654
655 H2W_DBG("");
656
657 mutex_lock(&hi->mutex_lock);
658 switch_set_state(&hi->sdev, switch_get_state(&hi->sdev) &
659 ~(BIT_HEADSET | BIT_HEADSET_NO_MIC));
660 mutex_unlock(&hi->mutex_lock);
661 hi->init_cpld();
662
663 /* Disable button */
664 switch (hi->htc_headset_flag) {
665 case H2W_HTC_HEADSET:
666 local_irq_save(irq_flags);
667 disable_irq(hi->irq_btn);
668 local_irq_restore(irq_flags);
669
670 if (atomic_read(&hi->btn_state))
671 button_released();
672 break;
673 case H2W_DEVICE:
674 h2w_dev_power_on(0);
675 set_irq_type(hi->irq_btn, IRQF_TRIGGER_LOW);
676 disable_irq(hi->irq_btn);
677 /* 10ms (5-15 with 10ms tick) */
678 hi->btn_debounce_time = ktime_set(0, 10000000);
679 hi->set_clk_dir(0);
680 hi->set_dat_dir(0);
681 break;
682 }
683
684 hi->htc_headset_flag = 0;
685 hi->debounce_time = ktime_set(0, 100000000); /* 100 ms */
686
687}
688
689#ifdef CONFIG_MSM_SERIAL_DEBUGGER
690extern void msm_serial_debug_enable(int);
691#endif
692
693static void insert_headset(int type)
694{
695 unsigned long irq_flags;
696 int state;
697
698 H2W_DBG("");
699
700 hi->htc_headset_flag = type;
701 state = BIT_HEADSET | BIT_HEADSET_NO_MIC;
702
703 state = switch_get_state(&hi->sdev);
704 state &= ~(BIT_HEADSET_NO_MIC | BIT_HEADSET);
705 switch (type) {
706 case H2W_HTC_HEADSET:
707 printk(KERN_INFO "insert_headset H2W_HTC_HEADSET\n");
708 state |= BIT_HEADSET;
709 hi->ignore_btn = !gpio_get_value(hi->cable_in2);
710 /* Enable button irq */
711 local_irq_save(irq_flags);
712 enable_irq(hi->irq_btn);
713 local_irq_restore(irq_flags);
714 hi->debounce_time = ktime_set(0, 200000000); /* 20 ms */
715 break;
716 case H2W_DEVICE:
717 if (h2w_dev_detect() < 0) {
718 printk(KERN_INFO "H2W_DEVICE -- Non detect\n");
719 remove_headset();
720 } else {
721 printk(KERN_INFO "H2W_DEVICE -- detect\n");
722 hi->btn_debounce_time = ktime_set(0, 0);
723 local_irq_save(irq_flags);
724 enable_irq(hi->irq_btn);
725 set_irq_type(hi->irq_btn, IRQF_TRIGGER_RISING);
726 local_irq_restore(irq_flags);
727 state |= BIT_HEADSET;
728 }
729 break;
730 case H2W_USB_CRADLE:
731 state |= BIT_HEADSET_NO_MIC;
732 break;
733 case H2W_UART_DEBUG:
734 hi->config_cpld(hi->debug_uart);
735 printk(KERN_INFO "switch to H2W_UART_DEBUG\n");
736 default:
737 return;
738 }
739 mutex_lock(&hi->mutex_lock);
740 switch_set_state(&hi->sdev, state);
741 mutex_unlock(&hi->mutex_lock);
742
743#ifdef CONFIG_MSM_SERIAL_DEBUGGER
744 msm_serial_debug_enable(false);
745#endif
746
747}
748#if 0
749static void remove_headset(void)
750{
751 unsigned long irq_flags;
752
753 H2W_DBG("");
754
755 switch_set_state(&hi->sdev, H2W_NO_DEVICE);
756
757 hi->init_cpld();
758
759 /* Disable button */
760 local_irq_save(irq_flags);
761 disable_irq(hi->irq_btn);
762 local_irq_restore(irq_flags);
763
764 if (atomic_read(&hi->btn_state))
765 button_released();
766
767 hi->debounce_time = ktime_set(0, 100000000); /* 100 ms */
768}
769#endif
770static int is_accessary_pluged_in(void)
771{
772 int type = 0;
773 int clk1 = 0, dat1 = 0, clk2 = 0, dat2 = 0, clk3 = 0, dat3 = 0;
774
775 /* Step1: save H2W_CLK and H2W_DAT */
776 /* Delay 10ms for pin stable. */
777 msleep(10);
778 clk1 = gpio_get_value(hi->h2w_clk);
779 dat1 = gpio_get_value(hi->h2w_data);
780
781 /*
782 * Step2: set GPIO_CABLE_IN1 as output high and GPIO_CABLE_IN2 as
783 * input
784 */
785 gpio_direction_output(hi->cable_in1, 1);
786 gpio_direction_input(hi->cable_in2);
787 /* Delay 10ms for pin stable. */
788 msleep(10);
789 /* Step 3: save H2W_CLK and H2W_DAT */
790 clk2 = gpio_get_value(hi->h2w_clk);
791 dat2 = gpio_get_value(hi->h2w_data);
792
793 /*
794 * Step 4: set GPIO_CABLE_IN1 as input and GPIO_CABLE_IN2 as output
795 * high
796 */
797 gpio_direction_input(hi->cable_in1);
798 gpio_direction_output(hi->cable_in2, 1);
799 /* Delay 10ms for pin stable. */
800 msleep(10);
801 /* Step 5: save H2W_CLK and H2W_DAT */
802 clk3 = gpio_get_value(hi->h2w_clk);
803 dat3 = gpio_get_value(hi->h2w_data);
804
805 /* Step 6: set both GPIO_CABLE_IN1 and GPIO_CABLE_IN2 as input */
806 gpio_direction_input(hi->cable_in1);
807 gpio_direction_input(hi->cable_in2);
808
809 H2W_DBG("(%d,%d) (%d,%d) (%d,%d)\n",
810 clk1, dat1, clk2, dat2, clk3, dat3);
811
812 if ((clk1 == 0) && (dat1 == 1) &&
813 (clk2 == 0) && (dat2 == 1) &&
814 (clk3 == 0) && (dat3 == 1))
815 type = H2W_HTC_HEADSET;
816 else if ((clk1 == 0) && (dat1 == 0) &&
817 (clk2 == 0) && (dat2 == 0) &&
818 (clk3 == 0) && (dat3 == 0))
819 type = NORMAL_HEARPHONE;
820 else if ((clk1 == 0) && (dat1 == 0) &&
821 (clk2 == 1) && (dat2 == 0) &&
822 (clk3 == 0) && (dat3 == 1))
823 type = H2W_DEVICE;
824 else if ((clk1 == 0) && (dat1 == 0) &&
825 (clk2 == 1) && (dat2 == 1) &&
826 (clk3 == 1) && (dat3 == 1))
827 type = H2W_USB_CRADLE;
828 else if ((clk1 == 0) && (dat1 == 1) &&
829 (clk2 == 1) && (dat2 == 1) &&
830 (clk3 == 0) && (dat3 == 1))
831 type = H2W_UART_DEBUG;
832 else
833 type = H2W_NO_DEVICE;
834
835 return type;
836}
837
838
839static void detection_work(struct work_struct *work)
840{
841 unsigned long irq_flags;
842 int type;
843
844 H2W_DBG("");
845
846 if (gpio_get_value(hi->cable_in1) != 0) {
847 /* Headset not plugged in */
848 if (switch_get_state(&hi->sdev) != H2W_NO_DEVICE)
849 remove_headset();
850 return;
851 }
852
853 /* Something plugged in, lets make sure its a headset */
854
855 /* Switch CPLD to GPIO to do detection */
856 hi->config_cpld(H2W_GPIO);
857
858 /* Disable headset interrupt while detecting.*/
859 local_irq_save(irq_flags);
860 disable_irq(hi->irq);
861 local_irq_restore(irq_flags);
862
863 /* Something plugged in, lets make sure its a headset */
864 type = is_accessary_pluged_in();
865
866 /* Restore IRQs */
867 local_irq_save(irq_flags);
868 enable_irq(hi->irq);
869 local_irq_restore(irq_flags);
870
871 insert_headset(type);
872}
873
874static enum hrtimer_restart button_event_timer_func(struct hrtimer *data)
875{
876 int key, press, keyname, h2w_key = 1;
877
878 H2W_DBG("");
879
880 if (switch_get_state(&hi->sdev) == H2W_HTC_HEADSET) {
881 switch (hi->htc_headset_flag) {
882 case H2W_HTC_HEADSET:
883 if (gpio_get_value(hi->cable_in2)) {
884 if (hi->ignore_btn)
885 hi->ignore_btn = 0;
886 else if (atomic_read(&hi->btn_state))
887 button_released();
888 } else {
889 if (!hi->ignore_btn &&
890 !atomic_read(&hi->btn_state))
891 button_pressed();
892 }
893 break;
894 case H2W_DEVICE:
895 if ((hi->get_dat() == 1) && (hi->get_clk() == 1)) {
896 /* Don't do anything because H2W pull out. */
897 H2WE("Remote Control pull out.\n");
898 } else {
899 key = h2w_get_fnkey();
900 press = (key > 0x7F) ? 0 : 1;
901 keyname = key & 0x7F;
902 /* H2WI("key = %d, press = %d,
903 keyname = %d \n",
904 key, press, keyname); */
905 switch (keyname) {
906 case H2W_KEY_PLAY:
907 H2WI("H2W_KEY_PLAY");
908 key = KEY_PLAYPAUSE;
909 break;
910 case H2W_KEY_FORWARD:
911 H2WI("H2W_KEY_FORWARD");
912 key = KEY_NEXTSONG;
913 break;
914 case H2W_KEY_BACKWARD:
915 H2WI("H2W_KEY_BACKWARD");
916 key = KEY_PREVIOUSSONG;
917 break;
918 case H2W_KEY_VOLUP:
919 H2WI("H2W_KEY_VOLUP");
920 key = KEY_VOLUMEUP;
921 break;
922 case H2W_KEY_VOLDOWN:
923 H2WI("H2W_KEY_VOLDOWN");
924 key = KEY_VOLUMEDOWN;
925 break;
926 case H2W_KEY_PICKUP:
927 H2WI("H2W_KEY_PICKUP");
928 key = KEY_SEND;
929 break;
930 case H2W_KEY_HANGUP:
931 H2WI("H2W_KEY_HANGUP");
932 key = KEY_END;
933 break;
934 case H2W_KEY_MUTE:
935 H2WI("H2W_KEY_MUTE");
936 key = KEY_MUTE;
937 break;
938 case H2W_KEY_HOLD:
939 H2WI("H2W_KEY_HOLD");
940 break;
941 default:
942 H2WI("default");
943 h2w_key = 0;
944 }
945 if (h2w_key) {
946 if (press)
947 H2WI("Press\n");
948 else
949 H2WI("Release\n");
950 input_report_key(hi->input, key, press);
951 }
952 }
953 break;
954 } /* end switch */
955 }
956
957 return HRTIMER_NORESTART;
958}
959
960static enum hrtimer_restart detect_event_timer_func(struct hrtimer *data)
961{
962 H2W_DBG("");
963
964 queue_work(g_detection_work_queue, &g_detection_work);
965 return HRTIMER_NORESTART;
966}
967
968static irqreturn_t detect_irq_handler(int irq, void *dev_id)
969{
970 int value1, value2;
971 int retry_limit = 10;
972
973 H2W_DBG("");
974 set_irq_type(hi->irq_btn, IRQF_TRIGGER_LOW);
975 do {
976 value1 = gpio_get_value(hi->cable_in1);
977 set_irq_type(hi->irq, value1 ?
978 IRQF_TRIGGER_LOW : IRQF_TRIGGER_HIGH);
979 value2 = gpio_get_value(hi->cable_in1);
980 } while (value1 != value2 && retry_limit-- > 0);
981
982 H2W_DBG("value2 = %d (%d retries), device=%d",
983 value2, (10-retry_limit), switch_get_state(&hi->sdev));
984
985 if ((switch_get_state(&hi->sdev) == H2W_NO_DEVICE) ^ value2) {
986 if (switch_get_state(&hi->sdev) == H2W_HTC_HEADSET)
987 hi->ignore_btn = 1;
988 /* Do the rest of the work in timer context */
989 hrtimer_start(&hi->timer, hi->debounce_time, HRTIMER_MODE_REL);
990 }
991
992 return IRQ_HANDLED;
993}
994
995static irqreturn_t button_irq_handler(int irq, void *dev_id)
996{
997 int value1, value2;
998 int retry_limit = 10;
999
1000 H2W_DBG("");
1001 do {
1002 value1 = gpio_get_value(hi->cable_in2);
1003 if (hi->htc_headset_flag != H2W_DEVICE)
1004 set_irq_type(hi->irq_btn, value1 ?
1005 IRQF_TRIGGER_LOW : IRQF_TRIGGER_HIGH);
1006 value2 = gpio_get_value(hi->cable_in2);
1007 } while (value1 != value2 && retry_limit-- > 0);
1008
1009 H2W_DBG("value2 = %d (%d retries)", value2, (10-retry_limit));
1010
1011 hrtimer_start(&hi->btn_timer, hi->btn_debounce_time, HRTIMER_MODE_REL);
1012
1013 return IRQ_HANDLED;
1014}
1015
1016#if defined(CONFIG_DEBUG_FS)
1017static int h2w_debug_set(void *data, u64 val)
1018{
1019 mutex_lock(&hi->mutex_lock);
1020 switch_set_state(&hi->sdev, (int)val);
1021 mutex_unlock(&hi->mutex_lock);
1022 return 0;
1023}
1024
1025static int h2w_debug_get(void *data, u64 *val)
1026{
1027 return 0;
1028}
1029
1030DEFINE_SIMPLE_ATTRIBUTE(h2w_debug_fops, h2w_debug_get, h2w_debug_set, "%llu\n");
1031static int __init h2w_debug_init(void)
1032{
1033 struct dentry *dent;
1034
1035 dent = debugfs_create_dir("h2w", 0);
1036 if (IS_ERR(dent))
1037 return PTR_ERR(dent);
1038
1039 debugfs_create_file("state", 0644, dent, NULL, &h2w_debug_fops);
1040
1041 return 0;
1042}
1043
1044device_initcall(h2w_debug_init);
1045#endif
1046
1047static int h2w_probe(struct platform_device *pdev)
1048{
1049 int ret;
1050 struct h2w_platform_data *pdata = pdev->dev.platform_data;
1051
1052 printk(KERN_INFO "H2W: Registering H2W (headset) driver\n");
1053 hi = kzalloc(sizeof(struct h2w_info), GFP_KERNEL);
1054 if (!hi)
1055 return -ENOMEM;
1056
1057 atomic_set(&hi->btn_state, 0);
1058 hi->ignore_btn = 0;
1059
1060 hi->debounce_time = ktime_set(0, 100000000); /* 100 ms */
1061 hi->btn_debounce_time = ktime_set(0, 10000000); /* 10 ms */
1062
1063 hi->htc_headset_flag = 0;
1064 hi->cable_in1 = pdata->cable_in1;
1065 hi->cable_in2 = pdata->cable_in2;
1066 hi->h2w_clk = pdata->h2w_clk;
1067 hi->h2w_data = pdata->h2w_data;
1068 hi->debug_uart = pdata->debug_uart;
1069 hi->config_cpld = pdata->config_cpld;
1070 hi->init_cpld = pdata->init_cpld;
1071 hi->set_dat = pdata->set_dat;
1072 hi->set_clk = pdata->set_clk;
1073 hi->set_dat_dir = pdata->set_dat_dir;
1074 hi->set_clk_dir = pdata->set_clk_dir;
1075 hi->get_dat = pdata->get_dat;
1076 hi->get_clk = pdata->get_clk;
1077 hi->speed = H2W_50KHz;
1078 /* obtain needed VREGs */
1079 if (pdata->power_name)
1080 hi->vreg_h2w = vreg_get(0, pdata->power_name);
1081
1082 mutex_init(&hi->mutex_lock);
1083
1084 hi->sdev.name = "h2w";
1085 hi->sdev.print_name = h2w_print_name;
1086
1087 ret = switch_dev_register(&hi->sdev);
1088 if (ret < 0)
1089 goto err_switch_dev_register;
1090
1091 g_detection_work_queue = create_workqueue("detection");
1092 if (g_detection_work_queue == NULL) {
1093 ret = -ENOMEM;
1094 goto err_create_work_queue;
1095 }
1096
1097 ret = gpio_request(hi->cable_in1, "h2w_detect");
1098 if (ret < 0)
1099 goto err_request_detect_gpio;
1100
1101 ret = gpio_request(hi->cable_in2, "h2w_button");
1102 if (ret < 0)
1103 goto err_request_button_gpio;
1104
1105 ret = gpio_direction_input(hi->cable_in1);
1106 if (ret < 0)
1107 goto err_set_detect_gpio;
1108
1109 ret = gpio_direction_input(hi->cable_in2);
1110 if (ret < 0)
1111 goto err_set_button_gpio;
1112
1113 hi->irq = gpio_to_irq(hi->cable_in1);
1114 if (hi->irq < 0) {
1115 ret = hi->irq;
1116 goto err_get_h2w_detect_irq_num_failed;
1117 }
1118
1119 hi->irq_btn = gpio_to_irq(hi->cable_in2);
1120 if (hi->irq_btn < 0) {
1121 ret = hi->irq_btn;
1122 goto err_get_button_irq_num_failed;
1123 }
1124
1125 /* Set CPLD MUX to H2W <-> CPLD GPIO */
1126 hi->init_cpld();
1127
1128 hrtimer_init(&hi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1129 hi->timer.function = detect_event_timer_func;
1130 hrtimer_init(&hi->btn_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1131 hi->btn_timer.function = button_event_timer_func;
1132
1133 ret = request_irq(hi->irq, detect_irq_handler,
1134 IRQF_TRIGGER_LOW, "h2w_detect", NULL);
1135 if (ret < 0)
1136 goto err_request_detect_irq;
1137
1138 /* Disable button until plugged in */
1139 set_irq_flags(hi->irq_btn, IRQF_VALID | IRQF_NOAUTOEN);
1140 ret = request_irq(hi->irq_btn, button_irq_handler,
1141 IRQF_TRIGGER_LOW, "h2w_button", NULL);
1142 if (ret < 0)
1143 goto err_request_h2w_headset_button_irq;
1144
1145 ret = set_irq_wake(hi->irq, 1);
1146 if (ret < 0)
1147 goto err_request_input_dev;
1148
1149 ret = set_irq_wake(hi->irq_btn, 1);
1150 if (ret < 0)
1151 goto err_request_input_dev;
1152
1153
1154
1155 hi->input = input_allocate_device();
1156 if (!hi->input) {
1157 ret = -ENOMEM;
1158 goto err_request_input_dev;
1159 }
1160
1161 hi->input->name = "h2w headset";
1162 set_bit(EV_SYN, hi->input->evbit);
1163 set_bit(EV_KEY, hi->input->evbit);
1164 set_bit(KEY_MEDIA, hi->input->keybit);
1165 set_bit(KEY_NEXTSONG, hi->input->keybit);
1166 set_bit(KEY_PLAYPAUSE, hi->input->keybit);
1167 set_bit(KEY_PREVIOUSSONG, hi->input->keybit);
1168 set_bit(KEY_MUTE, hi->input->keybit);
1169 set_bit(KEY_VOLUMEUP, hi->input->keybit);
1170 set_bit(KEY_VOLUMEDOWN, hi->input->keybit);
1171 set_bit(KEY_END, hi->input->keybit);
1172 set_bit(KEY_SEND, hi->input->keybit);
1173
1174 ret = input_register_device(hi->input);
1175 if (ret < 0)
1176 goto err_register_input_dev;
1177
1178 return 0;
1179
1180err_register_input_dev:
1181 input_free_device(hi->input);
1182err_request_input_dev:
1183 free_irq(hi->irq_btn, 0);
1184err_request_h2w_headset_button_irq:
1185 free_irq(hi->irq, 0);
1186err_request_detect_irq:
1187err_get_button_irq_num_failed:
1188err_get_h2w_detect_irq_num_failed:
1189err_set_button_gpio:
1190err_set_detect_gpio:
1191 gpio_free(hi->cable_in2);
1192err_request_button_gpio:
1193 gpio_free(hi->cable_in1);
1194err_request_detect_gpio:
1195 destroy_workqueue(g_detection_work_queue);
1196err_create_work_queue:
1197 switch_dev_unregister(&hi->sdev);
1198err_switch_dev_register:
1199 printk(KERN_ERR "H2W: Failed to register driver\n");
1200
1201 return ret;
1202}
1203
1204static int h2w_remove(struct platform_device *pdev)
1205{
1206 H2W_DBG("");
1207 if (switch_get_state(&hi->sdev))
1208 remove_headset();
1209 input_unregister_device(hi->input);
1210 gpio_free(hi->cable_in2);
1211 gpio_free(hi->cable_in1);
1212 free_irq(hi->irq_btn, 0);
1213 free_irq(hi->irq, 0);
1214 destroy_workqueue(g_detection_work_queue);
1215 switch_dev_unregister(&hi->sdev);
1216
1217 return 0;
1218}
1219
1220
1221static struct platform_driver h2w_driver = {
1222 .probe = h2w_probe,
1223 .remove = h2w_remove,
1224 .driver = {
1225 .name = "h2w",
1226 .owner = THIS_MODULE,
1227 },
1228};
1229
1230static int __init h2w_init(void)
1231{
1232 H2W_DBG("");
1233 return platform_driver_register(&h2w_driver);
1234}
1235
1236static void __exit h2w_exit(void)
1237{
1238 platform_driver_unregister(&h2w_driver);
1239}
1240
1241module_init(h2w_init);
1242module_exit(h2w_exit);
1243
1244MODULE_AUTHOR("Laurence Chen <Laurence_Chen@htc.com>");
1245MODULE_DESCRIPTION("HTC 2 Wire detection driver");
1246MODULE_LICENSE("GPL");