blob: aa83e216974dc4633661761e8f1f0e3abd73ebd0 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
2 * H2W device detection driver.
3 *
4 * Copyright (C) 2008 HTC Corporation.
5 * Copyright (C) 2008 Google, Inc.
6 *
7 * Authors:
8 * Laurence Chen <Laurence_Chen@htc.com>
9 * Nick Pelly <npelly@google.com>
10 *
11 * This software is licensed under the terms of the GNU General Public
12 * License version 2, as published by the Free Software Foundation, and
13 * may be copied, distributed, and modified under those terms.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 */
21
22/* For detecting HTC 2 Wire devices, such as wired headset.
23
24 Logically, the H2W driver is always present, and H2W state (hi->state)
25 indicates what is currently plugged into the H2W interface.
26
27 When the headset is plugged in, CABLE_IN1 is pulled low. When the headset
28 button is pressed, CABLE_IN2 is pulled low. These two lines are shared with
29 the TX and RX (respectively) of UART3 - used for serial debugging.
30
31 This headset driver keeps the CPLD configured as UART3 for as long as
32 possible, so that we can do serial FIQ debugging even when the kernel is
33 locked and this driver no longer runs. So it only configures the CPLD to
34 GPIO while the headset is plugged in, and for 10ms during detection work.
35
36 Unfortunately we can't leave the CPLD as UART3 while a headset is plugged
37 in, UART3 is pullup on TX but the headset is pull-down, causing a 55 mA
38 drain on sapphire.
39
40 The headset detection work involves setting CPLD to GPIO, and then pulling
41 CABLE_IN1 high with a stronger pullup than usual. A H2W headset will still
42 pull this line low, whereas other attachments such as a serial console
43 would get pulled up by this stronger pullup.
44
45 Headset insertion/removal causes UEvent's to be sent, and
46 /sys/class/switch/h2w/state to be updated.
47
48 Button presses are interpreted as input event (KEY_MEDIA). Button presses
49 are ignored if the headset is plugged in, so the buttons on 11 pin -> 3.5mm
50 jack adapters do not work until a headset is plugged into the adapter. This
51 is to avoid serial RX traffic causing spurious button press events.
52
53 We tend to check the status of CABLE_IN1 a few more times than strictly
54 necessary during headset detection, to avoid spurious headset insertion
55 events caused by serial debugger TX traffic.
56*/
57
58
59#include <linux/module.h>
60#include <linux/sysdev.h>
61#include <linux/fs.h>
62#include <linux/interrupt.h>
63#include <linux/workqueue.h>
64#include <linux/irq.h>
65#include <linux/delay.h>
66#include <linux/types.h>
67#include <linux/input.h>
68#include <linux/platform_device.h>
69#include <linux/mutex.h>
70#include <linux/errno.h>
71#include <linux/err.h>
72#include <linux/hrtimer.h>
73#include <linux/switch.h>
74#include <linux/input.h>
75#include <linux/debugfs.h>
76#include <linux/gpio.h>
77#include <asm/atomic.h>
78#include <mach/board.h>
79#include <mach/vreg.h>
80#include <asm/mach-types.h>
81#include "board-sapphire.h"
82
83#ifdef CONFIG_DEBUG_SAPPHIRE_H2W
84#define H2W_DBG(fmt, arg...) printk(KERN_INFO "[H2W] %s " fmt "\n", __FUNCTION__, ## arg)
85#else
86#define H2W_DBG(fmt, arg...) do {} while (0)
87#endif
88
89static struct workqueue_struct *g_detection_work_queue;
90static void detection_work(struct work_struct *work);
91static DECLARE_WORK(g_detection_work, detection_work);
92enum {
93 NO_DEVICE = 0,
94 HTC_HEADSET = 1,
95};
96
97enum {
98 UART3 = 0,
99 GPIO = 1,
100};
101
102struct h2w_info {
103 struct switch_dev sdev;
104 struct input_dev *input;
105
106 atomic_t btn_state;
107 int ignore_btn;
108
109 unsigned int irq;
110 unsigned int irq_btn;
111
112 struct hrtimer timer;
113 ktime_t debounce_time;
114
115 struct hrtimer btn_timer;
116 ktime_t btn_debounce_time;
117};
118static struct h2w_info *hi;
119
120static ssize_t sapphire_h2w_print_name(struct switch_dev *sdev, char *buf)
121{
122 switch (switch_get_state(&hi->sdev)) {
123 case NO_DEVICE:
124 return sprintf(buf, "No Device\n");
125 case HTC_HEADSET:
126 return sprintf(buf, "Headset\n");
127 }
128 return -EINVAL;
129}
130
131static void configure_cpld(int route)
132{
133 H2W_DBG(" route = %s", route == UART3 ? "UART3" : "GPIO");
134 switch (route) {
135 case UART3:
136 gpio_set_value(SAPPHIRE_GPIO_H2W_SEL0, 0);
137 gpio_set_value(SAPPHIRE_GPIO_H2W_SEL1, 1);
138 break;
139 case GPIO:
140 gpio_set_value(SAPPHIRE_GPIO_H2W_SEL0, 0);
141 gpio_set_value(SAPPHIRE_GPIO_H2W_SEL1, 0);
142 break;
143 }
144}
145
146static void button_pressed(void)
147{
148 H2W_DBG("");
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("");
157 atomic_set(&hi->btn_state, 0);
158 input_report_key(hi->input, KEY_MEDIA, 0);
159 input_sync(hi->input);
160}
161
162#ifdef CONFIG_MSM_SERIAL_DEBUGGER
163extern void msm_serial_debug_enable(int);
164#endif
165
166static void insert_headset(void)
167{
168 unsigned long irq_flags;
169
170 H2W_DBG("");
171
172 switch_set_state(&hi->sdev, HTC_HEADSET);
173 configure_cpld(GPIO);
174
175#ifdef CONFIG_MSM_SERIAL_DEBUGGER
176 msm_serial_debug_enable(false);
177#endif
178
179
180 /* On some non-standard headset adapters (usually those without a
181 * button) the btn line is pulled down at the same time as the detect
182 * line. We can check here by sampling the button line, if it is
183 * low then it is probably a bad adapter so ignore the button.
184 * If the button is released then we stop ignoring the button, so that
185 * the user can recover from the situation where a headset is plugged
186 * in with button held down.
187 */
188 hi->ignore_btn = !gpio_get_value(SAPPHIRE_GPIO_CABLE_IN2);
189
190 /* Enable button irq */
191 local_irq_save(irq_flags);
192 enable_irq(hi->irq_btn);
193 local_irq_restore(irq_flags);
194
195 hi->debounce_time = ktime_set(0, 20000000); /* 20 ms */
196}
197
198static void remove_headset(void)
199{
200 unsigned long irq_flags;
201
202 H2W_DBG("");
203
204 switch_set_state(&hi->sdev, NO_DEVICE);
205 configure_cpld(UART3);
206
207 /* Disable button */
208 local_irq_save(irq_flags);
209 disable_irq(hi->irq_btn);
210 local_irq_restore(irq_flags);
211
212 if (atomic_read(&hi->btn_state))
213 button_released();
214
215 hi->debounce_time = ktime_set(0, 100000000); /* 100 ms */
216}
217
218static void detection_work(struct work_struct *work)
219{
220 unsigned long irq_flags;
221 int clk, cable_in1;
222
223 H2W_DBG("");
224
225 if (gpio_get_value(SAPPHIRE_GPIO_CABLE_IN1) != 0) {
226 /* Headset not plugged in */
227 if (switch_get_state(&hi->sdev) == HTC_HEADSET)
228 remove_headset();
229 return;
230 }
231
232 /* Something plugged in, lets make sure its a headset */
233
234 /* Switch CPLD to GPIO to do detection */
235 configure_cpld(GPIO);
236 /* Disable headset interrupt while detecting.*/
237 local_irq_save(irq_flags);
238 disable_irq(hi->irq);
239 local_irq_restore(irq_flags);
240
241 /* Set GPIO_CABLE_IN1 as output high */
242 gpio_direction_output(SAPPHIRE_GPIO_CABLE_IN1, 1);
243 /* Delay 10ms for pin stable. */
244 msleep(10);
245 /* Save H2W_CLK */
246 clk = gpio_get_value(SAPPHIRE_GPIO_H2W_CLK_GPI);
247 /* Set GPIO_CABLE_IN1 as input */
248 gpio_direction_input(SAPPHIRE_GPIO_CABLE_IN1);
249
250 /* Restore IRQs */
251 local_irq_save(irq_flags);
252 enable_irq(hi->irq);
253 local_irq_restore(irq_flags);
254
255 cable_in1 = gpio_get_value(SAPPHIRE_GPIO_CABLE_IN1);
256
257 if (cable_in1 == 0 && clk == 0) {
258 if (switch_get_state(&hi->sdev) == NO_DEVICE)
259 insert_headset();
260 } else {
261 configure_cpld(UART3);
262 H2W_DBG("CABLE_IN1 was low, but not a headset "
263 "(recent cable_in1 = %d, clk = %d)", cable_in1, clk);
264 }
265}
266
267static enum hrtimer_restart button_event_timer_func(struct hrtimer *data)
268{
269 H2W_DBG("");
270
271 if (switch_get_state(&hi->sdev) == HTC_HEADSET) {
272 if (gpio_get_value(SAPPHIRE_GPIO_CABLE_IN2)) {
273 if (hi->ignore_btn)
274 hi->ignore_btn = 0;
275 else if (atomic_read(&hi->btn_state))
276 button_released();
277 } else {
278 if (!hi->ignore_btn && !atomic_read(&hi->btn_state))
279 button_pressed();
280 }
281 }
282
283 return HRTIMER_NORESTART;
284}
285
286static enum hrtimer_restart detect_event_timer_func(struct hrtimer *data)
287{
288 H2W_DBG("");
289
290 queue_work(g_detection_work_queue, &g_detection_work);
291 return HRTIMER_NORESTART;
292}
293
294static irqreturn_t detect_irq_handler(int irq, void *dev_id)
295{
296 int value1, value2;
297 int retry_limit = 10;
298
299 H2W_DBG("");
300 do {
301 value1 = gpio_get_value(SAPPHIRE_GPIO_CABLE_IN1);
302 set_irq_type(hi->irq, value1 ?
303 IRQF_TRIGGER_LOW : IRQF_TRIGGER_HIGH);
304 value2 = gpio_get_value(SAPPHIRE_GPIO_CABLE_IN1);
305 } while (value1 != value2 && retry_limit-- > 0);
306
307 H2W_DBG("value2 = %d (%d retries)", value2, (10-retry_limit));
308
309 if ((switch_get_state(&hi->sdev) == NO_DEVICE) ^ value2) {
310 if (switch_get_state(&hi->sdev) == HTC_HEADSET)
311 hi->ignore_btn = 1;
312 /* Do the rest of the work in timer context */
313 hrtimer_start(&hi->timer, hi->debounce_time, HRTIMER_MODE_REL);
314 }
315
316 return IRQ_HANDLED;
317}
318
319static irqreturn_t button_irq_handler(int irq, void *dev_id)
320{
321 int value1, value2;
322 int retry_limit = 10;
323
324 H2W_DBG("");
325 do {
326 value1 = gpio_get_value(SAPPHIRE_GPIO_CABLE_IN2);
327 set_irq_type(hi->irq_btn, value1 ?
328 IRQF_TRIGGER_LOW : IRQF_TRIGGER_HIGH);
329 value2 = gpio_get_value(SAPPHIRE_GPIO_CABLE_IN2);
330 } while (value1 != value2 && retry_limit-- > 0);
331
332 H2W_DBG("value2 = %d (%d retries)", value2, (10-retry_limit));
333
334 hrtimer_start(&hi->btn_timer, hi->btn_debounce_time, HRTIMER_MODE_REL);
335
336 return IRQ_HANDLED;
337}
338
339#if defined(CONFIG_DEBUG_FS)
340static void h2w_debug_set(void *data, u64 val)
341{
342 switch_set_state(&hi->sdev, (int)val);
343}
344
345static u64 h2w_debug_get(void *data)
346{
347 return 0;
348}
349
350DEFINE_SIMPLE_ATTRIBUTE(h2w_debug_fops, h2w_debug_get, h2w_debug_set, "%llu\n");
351static int __init h2w_debug_init(void)
352{
353 struct dentry *dent;
354
355 dent = debugfs_create_dir("h2w", 0);
356 if (IS_ERR(dent))
357 return PTR_ERR(dent);
358
359 debugfs_create_file("state", 0644, dent, NULL, &h2w_debug_fops);
360
361 return 0;
362}
363
364device_initcall(h2w_debug_init);
365#endif
366
367static int sapphire_h2w_probe(struct platform_device *pdev)
368{
369 int ret;
370 unsigned long irq_flags;
371
372 printk(KERN_INFO "H2W: Registering H2W (headset) driver\n");
373 hi = kzalloc(sizeof(struct h2w_info), GFP_KERNEL);
374 if (!hi)
375 return -ENOMEM;
376
377 atomic_set(&hi->btn_state, 0);
378 hi->ignore_btn = 0;
379
380 hi->debounce_time = ktime_set(0, 100000000); /* 100 ms */
381 hi->btn_debounce_time = ktime_set(0, 10000000); /* 10 ms */
382 hi->sdev.name = "h2w";
383 hi->sdev.print_name = sapphire_h2w_print_name;
384
385 ret = switch_dev_register(&hi->sdev);
386 if (ret < 0)
387 goto err_switch_dev_register;
388
389 g_detection_work_queue = create_workqueue("detection");
390 if (g_detection_work_queue == NULL) {
391 ret = -ENOMEM;
392 goto err_create_work_queue;
393 }
394
395 ret = gpio_request(SAPPHIRE_GPIO_CABLE_IN1, "h2w_detect");
396 if (ret < 0)
397 goto err_request_detect_gpio;
398
399 ret = gpio_request(SAPPHIRE_GPIO_CABLE_IN2, "h2w_button");
400 if (ret < 0)
401 goto err_request_button_gpio;
402
403 ret = gpio_direction_input(SAPPHIRE_GPIO_CABLE_IN1);
404 if (ret < 0)
405 goto err_set_detect_gpio;
406
407 ret = gpio_direction_input(SAPPHIRE_GPIO_CABLE_IN2);
408 if (ret < 0)
409 goto err_set_button_gpio;
410
411 hi->irq = gpio_to_irq(SAPPHIRE_GPIO_CABLE_IN1);
412 if (hi->irq < 0) {
413 ret = hi->irq;
414 goto err_get_h2w_detect_irq_num_failed;
415 }
416
417 hi->irq_btn = gpio_to_irq(SAPPHIRE_GPIO_CABLE_IN2);
418 if (hi->irq_btn < 0) {
419 ret = hi->irq_btn;
420 goto err_get_button_irq_num_failed;
421 }
422
423 /* Set CPLD MUX to H2W <-> CPLD GPIO */
424 configure_cpld(UART3);
425 /* Set the CPLD connected H2W GPIO's to input */
426 gpio_set_value(SAPPHIRE_GPIO_H2W_CLK_DIR, 0);
427 gpio_set_value(SAPPHIRE_GPIO_H2W_DAT_DIR, 0);
428
429 hrtimer_init(&hi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
430 hi->timer.function = detect_event_timer_func;
431 hrtimer_init(&hi->btn_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
432 hi->btn_timer.function = button_event_timer_func;
433
434 ret = request_irq(hi->irq, detect_irq_handler,
435 IRQF_TRIGGER_LOW, "h2w_detect", NULL);
436 if (ret < 0)
437 goto err_request_detect_irq;
438
439 /* Disable button until plugged in */
440 set_irq_flags(hi->irq_btn, IRQF_VALID | IRQF_NOAUTOEN);
441 ret = request_irq(hi->irq_btn, button_irq_handler,
442 IRQF_TRIGGER_LOW, "h2w_button", NULL);
443 if (ret < 0)
444 goto err_request_h2w_headset_button_irq;
445
446 ret = set_irq_wake(hi->irq, 1);
447 if (ret < 0)
448 goto err_request_input_dev;
449 ret = set_irq_wake(hi->irq_btn, 1);
450 if (ret < 0)
451 goto err_request_input_dev;
452
453 hi->input = input_allocate_device();
454 if (!hi->input) {
455 ret = -ENOMEM;
456 goto err_request_input_dev;
457 }
458
459 hi->input->name = "h2w headset";
460 hi->input->evbit[0] = BIT_MASK(EV_KEY);
461 hi->input->keybit[BIT_WORD(KEY_MEDIA)] = BIT_MASK(KEY_MEDIA);
462
463 ret = input_register_device(hi->input);
464 if (ret < 0)
465 goto err_register_input_dev;
466
467 return 0;
468
469err_register_input_dev:
470 input_free_device(hi->input);
471err_request_input_dev:
472 free_irq(hi->irq_btn, 0);
473err_request_h2w_headset_button_irq:
474 free_irq(hi->irq, 0);
475err_request_detect_irq:
476err_get_button_irq_num_failed:
477err_get_h2w_detect_irq_num_failed:
478err_set_button_gpio:
479err_set_detect_gpio:
480 gpio_free(SAPPHIRE_GPIO_CABLE_IN2);
481err_request_button_gpio:
482 gpio_free(SAPPHIRE_GPIO_CABLE_IN1);
483err_request_detect_gpio:
484 destroy_workqueue(g_detection_work_queue);
485err_create_work_queue:
486 switch_dev_unregister(&hi->sdev);
487err_switch_dev_register:
488 printk(KERN_ERR "H2W: Failed to register driver\n");
489
490 return ret;
491}
492
493static int sapphire_h2w_remove(struct platform_device *pdev)
494{
495 H2W_DBG("");
496 if (switch_get_state(&hi->sdev))
497 remove_headset();
498 input_unregister_device(hi->input);
499 gpio_free(SAPPHIRE_GPIO_CABLE_IN2);
500 gpio_free(SAPPHIRE_GPIO_CABLE_IN1);
501 free_irq(hi->irq_btn, 0);
502 free_irq(hi->irq, 0);
503 destroy_workqueue(g_detection_work_queue);
504 switch_dev_unregister(&hi->sdev);
505
506 return 0;
507}
508
509static struct platform_device sapphire_h2w_device = {
510 .name = "sapphire-h2w",
511};
512
513static struct platform_driver sapphire_h2w_driver = {
514 .probe = sapphire_h2w_probe,
515 .remove = sapphire_h2w_remove,
516 .driver = {
517 .name = "sapphire-h2w",
518 .owner = THIS_MODULE,
519 },
520};
521
522static int __init sapphire_h2w_init(void)
523{
524 if (!machine_is_sapphire())
525 return 0;
526 int ret;
527 H2W_DBG("");
528 ret = platform_driver_register(&sapphire_h2w_driver);
529 if (ret)
530 return ret;
531 return platform_device_register(&sapphire_h2w_device);
532}
533
534static void __exit sapphire_h2w_exit(void)
535{
536 platform_device_unregister(&sapphire_h2w_device);
537 platform_driver_unregister(&sapphire_h2w_driver);
538}
539
540module_init(sapphire_h2w_init);
541module_exit(sapphire_h2w_exit);
542
543MODULE_AUTHOR("Laurence Chen <Laurence_Chen@htc.com>");
544MODULE_DESCRIPTION("HTC 2 Wire detection driver for sapphire");
545MODULE_LICENSE("GPL");