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
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (c) 2009 by Michael Sevakis
*
* Driver to handle headphone jack events
*
* 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; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "config.h"
#include "system.h"
#include "kernel.h"
#include "thread.h"
#include "mc13783.h"
#include "mc13783-target.h"
#include "adc.h"
#include "button.h"
static struct wakeup headphone_wakeup;
static unsigned int headphone_thread_id;
static int headphone_stack[160/sizeof(int)]; /* Not much stack needed */
static const char * const headphone_thread_name = "headphone";
static bool headphones_detect = false;
/* Convert ADC reading into a button value. */
static int adc_data_to_button(unsigned int data)
{
int btn = BUTTON_NONE;
if (data < 505)
{
if (data < 252)
{
if (data < 149)
{
if (data >= 64)
{
/* Play/Pause */
btn = BUTTON_RC_PLAY;
}
/* else headphone direct */
}
else
{
/* DSP */
btn = BUTTON_RC_DSP;
}
}
else
{
if (data < 370)
{
/* RW */
btn = BUTTON_RC_REW;
}
else
{
/* FF */
btn = BUTTON_RC_FF;
}
}
}
else
{
if (data < 870)
{
if (data < 675)
{
/* Vol + */
btn = BUTTON_RC_VOL_UP;
}
else
{
/* Vol - */
btn = BUTTON_RC_VOL_DOWN;
}
}
#if 0
else
{
if (data < 951)
{
/* No buttons */
}
else
{
/* Not inserted */
}
}
#endif
}
return btn;
}
static void headphone_thread(void)
{
int headphone_sleep_countdown = 0;
int headphone_wait_timeout = TIMEOUT_BLOCK;
while (1)
{
int rc = wakeup_wait(&headphone_wakeup, headphone_wait_timeout);
unsigned int data = adc_read(ADC_HPREMOTE);
if (rc == OBJ_WAIT_TIMEDOUT)
{
if (headphone_sleep_countdown <= 0)
{
/* Polling ADC */
int btn, btn2;
btn = adc_data_to_button(data);
sleep(HZ/50);
data = adc_read(ADC_HPREMOTE);
btn2 = adc_data_to_button(data);
if (btn != btn2)
{
/* If the buttons dont agree twice in a row, then it's
* none (from meg-fx remote reader). */
btn = BUTTON_NONE;
}
button_headphone_set(btn);
continue;
}
if (--headphone_sleep_countdown == 0)
{
/* Nothing has changed and remote is not present -
* go to sleep. */
headphone_wait_timeout = TIMEOUT_BLOCK;
continue;
}
}
headphones_detect = data <= 951; /* Max remote value */
/* Cancel any buttons if jack readings are unstable. */
button_headphone_set(BUTTON_NONE);
if (data >= 64 && data <= 951)
{
/* Should be a remote control - accelerate */
headphone_wait_timeout = HZ/20-HZ/50;
headphone_sleep_countdown = 0;
}
else if (rc == OBJ_WAIT_SUCCEEDED)
{
/* Got signaled - something is being plugged/unplugged. Set
* countdown until we just give up and go to sleep (~10s). */
headphone_wait_timeout = HZ/2;
headphone_sleep_countdown = 10*2;
}
}
}
/* This is called from the mc13783 interrupt thread */
void headphone_detect_event(void)
{
/* Trigger the thread immediately. */
wakeup_signal(&headphone_wakeup);
}
/* Tell if anything is in the jack. */
bool headphones_inserted(void)
{
return headphones_detect;
}
void headphone_init(void)
{
/* A thread is required to monitor the remote ADC and jack state. */
wakeup_init(&headphone_wakeup);
headphone_thread_id = create_thread(headphone_thread,
headphone_stack,
sizeof(headphone_stack),
0, headphone_thread_name
IF_PRIO(, PRIORITY_REALTIME)
IF_COP(, CPU));
/* Initially poll and then enable PMIC event */
headphone_detect_event();
mc13783_enable_event(MC13783_ONOFD2_EVENT);
}
|