1 /* arch/arm/mach-rockchip/rk28_headset.c
2 *
3 * Copyright (C) 2009 Rockchip Corporation.
4 *
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15
16 #include <linux/module.h>
17 #include <linux/device.h>
18 #include <linux/fs.h>
19 #include <linux/interrupt.h>
20 #include <linux/workqueue.h>
21 #include <linux/irq.h>
22 #include <linux/delay.h>
23 #include <linux/types.h>
24 #include <linux/input.h>
25 #include <linux/platform_device.h>
26 #include <linux/mutex.h>
27 #include <linux/errno.h>
28 #include <linux/err.h>
29 #include <linux/hrtimer.h>
30 #include <linux/extcon-provider.h>
31 #include <linux/debugfs.h>
32 #include <linux/wakelock.h>
33 #include <linux/gpio.h>
34 #include <linux/of_gpio.h>
35 #include <asm/atomic.h>
36 #include <linux/pm.h>
37 #include <linux/i2c.h>
38 #include <linux/spi/spi.h>
39 #include "rk_headset.h"
40 #ifdef CONFIG_HAS_EARLYSUSPEND
41 #include <linux/earlysuspend.h>
42 #endif
43
44 /* Debug */
45 #if 0
46 #define DBG(x...) printk(x)
47 #else
48 #define DBG(x...) do { } while (0)
49 #endif
50
51 #define BIT_HEADSET BIT(0)
52 #define BIT_HEADSET_NO_MIC BIT(1)
53
54 #define HEADSET 0
55 #define HOOK 1
56
57 #define HEADSET_IN 1
58 #define HEADSET_OUT 0
59 #define HOOK_DOWN 1
60 #define HOOK_UP 0
61 #define enable 1
62 #define disable 0
63
64 #if defined(CONFIG_SND_RK_SOC_RK2928) || defined(CONFIG_SND_RK29_SOC_RK610)
65 extern void rk2928_codec_set_spk(bool on);
66 #endif
67 #ifdef CONFIG_SND_SOC_WM8994
68 extern int wm8994_set_status(void);
69 #endif
70
71 /* headset private data */
72 struct headset_priv {
73 struct input_dev *input_dev;
74 struct rk_headset_pdata *pdata;
75 unsigned int headset_status : 1;
76 unsigned int hook_status : 1;
77 unsigned int isMic : 1;
78 unsigned int isHook_irq : 1;
79 int cur_headset_status;
80 unsigned int irq[2];
81 unsigned int irq_type[2];
82 struct delayed_work h_delayed_work[2];
83 struct extcon_dev *edev;
84 struct mutex mutex_lock[2];
85 struct timer_list headset_timer;
86 unsigned char *keycodes;
87 };
88
89 static struct headset_priv *headset_info;
90
91 #ifdef CONFIG_MODEM_MIC_SWITCH
92 #define HP_MIC 0
93 #define MAIN_MIC 1
94
Modem_Mic_switch(int value)95 void Modem_Mic_switch(int value)
96 {
97 if (value == HP_MIC)
98 gpio_set_value(headset_info->pdata->mic_switch_gpio,
99 headset_info->pdata->hp_mic_io_value);
100 else if (value == MAIN_MIC)
101 gpio_set_value(headset_info->pdata->mic_switch_gpio,
102 headset_info->pdata->main_mic_io_value);
103 }
104
Modem_Mic_release(void)105 void Modem_Mic_release(void)
106 {
107 if (headset_info->cur_headset_status == 1)
108 gpio_set_value(headset_info->pdata->mic_switch_gpio,
109 headset_info->pdata->hp_mic_io_value);
110 else
111 gpio_set_value(headset_info->pdata->mic_switch_gpio,
112 headset_info->pdata->main_mic_io_value);
113 }
114 #endif
115
read_gpio(int gpio)116 static int read_gpio(int gpio)
117 {
118 int i, level;
119
120 for (i = 0; i < 3; i++) {
121 level = gpio_get_value(gpio);
122 if (level < 0) {
123 pr_warn("%s:get pin level again,pin=%d,i=%d\n",
124 __func__, gpio, i);
125 msleep(1);
126 continue;
127 } else
128 break;
129 }
130 if (level < 0)
131 pr_err("%s:get pin level err!\n", __func__);
132 return level;
133 }
134
headset_interrupt(int irq,void * dev_id)135 static irqreturn_t headset_interrupt(int irq, void *dev_id)
136 {
137 DBG("---headset_interrupt---\n");
138 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET],
139 msecs_to_jiffies(50));
140 return IRQ_HANDLED;
141 }
142
hook_interrupt(int irq,void * dev_id)143 static irqreturn_t hook_interrupt(int irq, void *dev_id)
144 {
145 DBG("---Hook_interrupt---\n");
146 /* disable_irq_nosync(headset_info->irq[HOOK]); */
147 schedule_delayed_work(&headset_info->h_delayed_work[HOOK],
148 msecs_to_jiffies(100));
149 return IRQ_HANDLED;
150 }
151
headsetobserve_work(struct work_struct * work)152 static void headsetobserve_work(struct work_struct *work)
153 {
154 int level = 0;
155 int level2 = 0;
156 struct rk_headset_pdata *pdata = headset_info->pdata;
157 static unsigned int old_status = 0;
158
159 printk("---headsetobserve_work---\n");
160 mutex_lock(&headset_info->mutex_lock[HEADSET]);
161 level = read_gpio(pdata->headset_gpio);
162 if (level < 0)
163 goto out;
164 msleep(100);
165 level2 = read_gpio(pdata->headset_gpio);
166 if (level2 < 0)
167 goto out;
168 if (level2 != level)
169 goto out;
170 old_status = headset_info->headset_status;
171 if (pdata->headset_insert_type == HEADSET_IN_HIGH)
172 headset_info->headset_status = level ? HEADSET_IN : HEADSET_OUT;
173 else
174 headset_info->headset_status = level ? HEADSET_OUT : HEADSET_IN;
175
176 if (old_status == headset_info->headset_status) {
177 pr_warn("old_status == headset_info->headset_status\n");
178 goto out;
179 }
180 DBG("(headset in is %s)headset status is %s\n",
181 pdata->headset_insert_type ? "high level" : "low level",
182 headset_info->headset_status ? "in" : "out");
183 if (headset_info->headset_status == HEADSET_IN) {
184 headset_info->cur_headset_status = BIT_HEADSET_NO_MIC;
185 if (pdata->headset_insert_type == HEADSET_IN_HIGH)
186 irq_set_irq_type(headset_info->irq[HEADSET],
187 IRQF_TRIGGER_FALLING);
188 else
189 irq_set_irq_type(headset_info->irq[HEADSET],
190 IRQF_TRIGGER_RISING);
191 if (pdata->hook_gpio) {
192 /* Start the timer, wait for switch to the headphone channel */
193 del_timer(&headset_info->headset_timer);
194 headset_info->headset_timer.expires = jiffies + 100;
195 add_timer(&headset_info->headset_timer);
196 goto out;
197 }
198 } else if (headset_info->headset_status == HEADSET_OUT) {
199 headset_info->hook_status = HOOK_UP;
200 if (headset_info->isHook_irq == enable) {
201 DBG("disable headset_hook irq\n");
202 headset_info->isHook_irq = disable;
203 disable_irq(headset_info->irq[HOOK]);
204 }
205 headset_info->cur_headset_status = 0;
206 if (pdata->headset_insert_type == HEADSET_IN_HIGH)
207 irq_set_irq_type(headset_info->irq[HEADSET],
208 IRQF_TRIGGER_RISING);
209 else
210 irq_set_irq_type(headset_info->irq[HEADSET],
211 IRQF_TRIGGER_FALLING);
212 }
213 if (headset_info->cur_headset_status)
214 extcon_set_state_sync(headset_info->edev, EXTCON_JACK_HEADPHONE,
215 true);
216 else
217 extcon_set_state_sync(headset_info->edev, EXTCON_JACK_HEADPHONE,
218 false);
219 DBG("headset_info->cur_headset_status = %d\n",
220 headset_info->cur_headset_status);
221 out:
222 mutex_unlock(&headset_info->mutex_lock[HEADSET]);
223 }
224
hook_work(struct work_struct * work)225 static void hook_work(struct work_struct *work)
226 {
227 int level = 0;
228 struct rk_headset_pdata *pdata = headset_info->pdata;
229 static unsigned int old_status = HOOK_UP;
230
231 mutex_lock(&headset_info->mutex_lock[HOOK]);
232 if (headset_info->headset_status == HEADSET_OUT) {
233 DBG("Headset is out\n");
234 goto RE_ERROR;
235 }
236 level = read_gpio(pdata->hook_gpio);
237 if (level < 0)
238 goto RE_ERROR;
239 old_status = headset_info->hook_status;
240 DBG("Hook_work -- level = %d\n", level);
241 if (level == 0)
242 headset_info->hook_status =
243 pdata->hook_down_type == HOOK_DOWN_HIGH ? HOOK_UP :
244 HOOK_DOWN;
245 else if (level > 0)
246 headset_info->hook_status =
247 pdata->hook_down_type == HOOK_DOWN_HIGH ? HOOK_DOWN :
248 HOOK_UP;
249 if (old_status == headset_info->hook_status) {
250 DBG("old_status == headset_info->hook_status\n");
251 goto RE_ERROR;
252 }
253 DBG("Hook_work -- level = %d hook status is %s\n", level,
254 headset_info->hook_status ? "key down" : "key up");
255 if (headset_info->hook_status == HOOK_DOWN) {
256 if (pdata->hook_down_type == HOOK_DOWN_HIGH)
257 irq_set_irq_type(headset_info->irq[HOOK],
258 IRQF_TRIGGER_FALLING);
259 else
260 irq_set_irq_type(headset_info->irq[HOOK],
261 IRQF_TRIGGER_RISING);
262 } else {
263 if (pdata->hook_down_type == HOOK_DOWN_HIGH)
264 irq_set_irq_type(headset_info->irq[HOOK],
265 IRQF_TRIGGER_RISING);
266 else
267 irq_set_irq_type(headset_info->irq[HOOK],
268 IRQF_TRIGGER_FALLING);
269 }
270 input_report_key(headset_info->input_dev, HOOK_KEY_CODE,
271 headset_info->hook_status);
272 input_sync(headset_info->input_dev);
273 RE_ERROR:
274 mutex_unlock(&headset_info->mutex_lock[HOOK]);
275 }
276
headset_timer_callback(struct timer_list * t)277 static void headset_timer_callback(struct timer_list *t)
278 {
279 struct headset_priv *headset = from_timer(headset, t, headset_timer);
280 struct rk_headset_pdata *pdata = headset->pdata;
281 int level = 0;
282
283 pr_info("headset_timer_callback, headset->headset_status %d\n",
284 headset->headset_status);
285 if (headset->headset_status == HEADSET_OUT) {
286 pr_info("Headset is out\n");
287 goto out;
288 }
289 level = read_gpio(pdata->hook_gpio);
290 if (level < 0)
291 goto out;
292 if ((level > 0 && pdata->hook_down_type == HOOK_DOWN_LOW) ||
293 (level == 0 && pdata->hook_down_type == HOOK_DOWN_HIGH)) {
294 headset->isMic = 1;
295 enable_irq(headset_info->irq[HOOK]);
296 headset->isHook_irq = enable;
297 headset_info->hook_status = HOOK_UP;
298 if (pdata->hook_down_type == HOOK_DOWN_HIGH)
299 irq_set_irq_type(headset_info->irq[HOOK],
300 IRQF_TRIGGER_RISING);
301 else
302 irq_set_irq_type(headset_info->irq[HOOK],
303 IRQF_TRIGGER_FALLING);
304 } else {
305 headset->isMic = 0;
306 }
307 pr_info("headset->isMic = %d\n", headset->isMic);
308 headset_info->cur_headset_status = headset_info->isMic ? 1 : 2;
309 if (headset->isMic == 1)
310 extcon_set_state_sync(headset_info->edev,
311 EXTCON_JACK_MICROPHONE, true);
312 else
313 extcon_set_state_sync(headset_info->edev,
314 EXTCON_JACK_MICROPHONE, false);
315 DBG("headset_info->cur_headset_status = %d\n",
316 headset_info->cur_headset_status);
317 out:
318 return;
319 }
320
321 #ifdef CONFIG_HAS_EARLYSUSPEND
headset_early_resume(struct early_suspend * h)322 static void headset_early_resume(struct early_suspend *h)
323 {
324 schedule_delayed_work(&headset_info->h_delayed_work[HEADSET],
325 msecs_to_jiffies(10));
326 //DBG(">>>>>headset_early_resume\n");
327 }
328
329 static struct early_suspend hs_early_suspend;
330 #endif
331
rk_hskey_open(struct input_dev * dev)332 static int rk_hskey_open(struct input_dev *dev)
333 {
334 //struct rk28_adckey *adckey = input_get_drvdata(dev);
335 //DBG("===========rk_Hskey_open===========\n");
336 return 0;
337 }
338
rk_hskey_close(struct input_dev * dev)339 static void rk_hskey_close(struct input_dev *dev)
340 {
341 //DBG("===========rk_Hskey_close===========\n");
342 //struct rk28_adckey *adckey = input_get_drvdata(dev);
343 }
344
345 static const unsigned int headset_cable[] = {
346 EXTCON_JACK_MICROPHONE,
347 EXTCON_JACK_HEADPHONE,
348 EXTCON_NONE,
349 };
350
rk_headset_probe(struct platform_device * pdev,struct rk_headset_pdata * pdata)351 int rk_headset_probe(struct platform_device *pdev,
352 struct rk_headset_pdata *pdata)
353 {
354 int ret = 0;
355 struct headset_priv *headset;
356
357 headset = devm_kzalloc(&pdev->dev, sizeof(*headset), GFP_KERNEL);
358 if (!headset) {
359 dev_err(&pdev->dev, "failed to allocate driver data\n");
360 return -ENOMEM;
361 }
362 headset_info = headset;
363 headset->pdata = pdata;
364 headset->headset_status = HEADSET_OUT;
365 headset->hook_status = HOOK_UP;
366 headset->isHook_irq = disable;
367 headset->cur_headset_status = 0;
368 headset->edev = devm_extcon_dev_allocate(&pdev->dev, headset_cable);
369 if (IS_ERR(headset->edev)) {
370 dev_err(&pdev->dev, "failed to allocate extcon device\n");
371 ret = -ENOMEM;
372 goto failed;
373 }
374 ret = devm_extcon_dev_register(&pdev->dev, headset->edev);
375 if (ret < 0) {
376 dev_err(&pdev->dev, "extcon_dev_register() failed: %d\n", ret);
377 goto failed;
378 }
379 mutex_init(&headset->mutex_lock[HEADSET]);
380 mutex_init(&headset->mutex_lock[HOOK]);
381 INIT_DELAYED_WORK(&headset->h_delayed_work[HEADSET],
382 headsetobserve_work);
383 INIT_DELAYED_WORK(&headset->h_delayed_work[HOOK], hook_work);
384 headset->isMic = 0;
385 timer_setup(&headset->headset_timer, headset_timer_callback, 0);
386 /* Create and register the input driver */
387 headset->input_dev = devm_input_allocate_device(&pdev->dev);
388 if (!headset->input_dev) {
389 dev_err(&pdev->dev, "failed to allocate input device\n");
390 ret = -ENOMEM;
391 goto failed;
392 }
393 headset->input_dev->name = pdev->name;
394 headset->input_dev->open = rk_hskey_open;
395 headset->input_dev->close = rk_hskey_close;
396 headset->input_dev->dev.parent = &pdev->dev;
397 /* input_dev->phys = KEY_PHYS_NAME; */
398 headset->input_dev->id.vendor = 0x0001;
399 headset->input_dev->id.product = 0x0001;
400 headset->input_dev->id.version = 0x0100;
401 /* Register the input device */
402 ret = input_register_device(headset->input_dev);
403 if (ret) {
404 dev_err(&pdev->dev, "failed to register input device\n");
405 goto failed;
406 }
407 input_set_capability(headset->input_dev, EV_KEY, HOOK_KEY_CODE);
408 #ifdef CONFIG_HAS_EARLYSUSPEND
409 hs_early_suspend.suspend = NULL;
410 hs_early_suspend.resume = headset_early_resume;
411 hs_early_suspend.level = ~0x0;
412 register_early_suspend(&hs_early_suspend);
413 #endif
414 if (pdata->headset_gpio) {
415 headset->irq[HEADSET] = gpio_to_irq(pdata->headset_gpio);
416 if (pdata->headset_insert_type == HEADSET_IN_HIGH)
417 headset->irq_type[HEADSET] = IRQF_TRIGGER_RISING;
418 else
419 headset->irq_type[HEADSET] = IRQF_TRIGGER_FALLING;
420 ret = devm_request_irq(&pdev->dev, headset->irq[HEADSET],
421 headset_interrupt,
422 headset->irq_type[HEADSET],
423 "headset_input", NULL);
424 if (ret)
425 goto failed;
426 if (pdata->headset_wakeup)
427 enable_irq_wake(headset->irq[HEADSET]);
428 } else {
429 dev_err(&pdev->dev, "failed init headset, please full headset_gpio function in board\n");
430 ret = -EEXIST;
431 goto failed;
432 }
433 if (pdata->hook_gpio) {
434 headset->irq[HOOK] = gpio_to_irq(pdata->hook_gpio);
435 headset->irq_type[HOOK] =
436 pdata->hook_down_type == HOOK_DOWN_HIGH ?
437 IRQF_TRIGGER_RISING :
438 IRQF_TRIGGER_FALLING;
439 ret = devm_request_irq(&pdev->dev, headset->irq[HOOK],
440 hook_interrupt, headset->irq_type[HOOK],
441 "headset_hook", NULL);
442 if (ret)
443 goto failed;
444 disable_irq(headset->irq[HOOK]);
445 }
446 schedule_delayed_work(&headset->h_delayed_work[HEADSET],
447 msecs_to_jiffies(500));
448 return 0;
449 failed:
450 dev_err(&pdev->dev, "failed to headset probe ret=%d\n", ret);
451 return ret;
452 }
453 EXPORT_SYMBOL_GPL(rk_headset_probe);
454
455 MODULE_LICENSE("GPL");
456