1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
4 #include <linux/module.h>
5 #include <linux/init.h>
6 #include <linux/slab.h>
7 #include <linux/device.h>
8 #include <linux/pm_runtime.h>
9 #include <linux/printk.h>
10 #include <linux/delay.h>
11 #include <linux/kernel.h>
12 #include <sound/soc.h>
13 #include <sound/jack.h>
14 #include "wcd-mbhc-v2.h"
16 #define HS_DETECT_PLUG_TIME_MS (3 * 1000)
17 #define MBHC_BUTTON_PRESS_THRESHOLD_MIN 250
18 #define GND_MIC_SWAP_THRESHOLD 4
19 #define WCD_FAKE_REMOVAL_MIN_PERIOD_MS 100
20 #define HPHL_CROSS_CONN_THRESHOLD 100
21 #define HS_VREF_MIN_VAL 1400
22 #define FAKE_REM_RETRY_ATTEMPTS 3
23 #define WCD_MBHC_ADC_HS_THRESHOLD_MV 1700
24 #define WCD_MBHC_ADC_HPH_THRESHOLD_MV 75
25 #define WCD_MBHC_ADC_MICBIAS_MV 1800
26 #define WCD_MBHC_FAKE_INS_RETRY 4
28 #define WCD_MBHC_JACK_MASK (SND_JACK_HEADSET | SND_JACK_LINEOUT | \
31 #define WCD_MBHC_JACK_BUTTON_MASK (SND_JACK_BTN_0 | SND_JACK_BTN_1 | \
32 SND_JACK_BTN_2 | SND_JACK_BTN_3 | \
33 SND_JACK_BTN_4 | SND_JACK_BTN_5)
35 enum wcd_mbhc_adc_mux_ctl {
47 struct snd_soc_component *component;
48 struct snd_soc_jack *jack;
49 struct wcd_mbhc_config *cfg;
50 const struct wcd_mbhc_cb *mbhc_cb;
51 const struct wcd_mbhc_intr *intr_ids;
52 struct wcd_mbhc_field *fields;
53 /* Delayed work to report long button press */
54 struct delayed_work mbhc_btn_dwork;
55 /* Work to correct accessory type */
56 struct work_struct correct_plug_swch;
59 u32 hph_status; /* track headhpone status */
62 bool in_swch_irq_handler;
63 bool hs_detect_work_stop;
65 bool extn_cable_hph_rem;
67 bool impedance_detect;
68 unsigned long event_state;
69 unsigned long jiffies_atreport;
70 /* impedance of hphl and hphr */
72 /* Holds type of Headset - Mono/Stereo */
73 enum wcd_mbhc_hph_type hph_type;
74 /* Holds mbhc detection method - ADC/Legacy */
75 int mbhc_detection_logic;
78 static inline int wcd_mbhc_write_field(const struct wcd_mbhc *mbhc,
81 if (!mbhc->fields[field].reg)
84 return snd_soc_component_write_field(mbhc->component,
85 mbhc->fields[field].reg,
86 mbhc->fields[field].mask, val);
89 static inline int wcd_mbhc_read_field(const struct wcd_mbhc *mbhc, int field)
91 if (!mbhc->fields[field].reg)
94 return snd_soc_component_read_field(mbhc->component,
95 mbhc->fields[field].reg,
96 mbhc->fields[field].mask);
99 static void wcd_program_hs_vref(struct wcd_mbhc *mbhc)
101 u32 reg_val = ((mbhc->cfg->v_hs_max - HS_VREF_MIN_VAL) / 100);
103 wcd_mbhc_write_field(mbhc, WCD_MBHC_HS_VREF, reg_val);
106 static void wcd_program_btn_threshold(const struct wcd_mbhc *mbhc, bool micbias)
108 struct snd_soc_component *component = mbhc->component;
110 mbhc->mbhc_cb->set_btn_thr(component, mbhc->cfg->btn_low,
112 mbhc->cfg->num_btn, micbias);
115 static void wcd_mbhc_curr_micbias_control(const struct wcd_mbhc *mbhc,
116 const enum wcd_mbhc_cs_mb_en_flag cs_mb_en)
120 * Some codecs handle micbias/pullup enablement in codec
121 * drivers itself and micbias is not needed for regular
122 * plug type detection. So if micbias_control callback function
123 * is defined, just return.
125 if (mbhc->mbhc_cb->mbhc_micbias_control)
130 wcd_mbhc_write_field(mbhc, WCD_MBHC_MICB_CTRL, 0);
131 wcd_mbhc_write_field(mbhc, WCD_MBHC_BTN_ISRC_CTL, 3);
132 /* Program Button threshold registers as per CS */
133 wcd_program_btn_threshold(mbhc, false);
136 wcd_mbhc_write_field(mbhc, WCD_MBHC_BTN_ISRC_CTL, 0);
137 wcd_mbhc_write_field(mbhc, WCD_MBHC_FSM_EN, 1);
138 /* Disable PULL_UP_EN & enable MICBIAS */
139 wcd_mbhc_write_field(mbhc, WCD_MBHC_MICB_CTRL, 2);
140 /* Program Button threshold registers as per MICBIAS */
141 wcd_program_btn_threshold(mbhc, true);
143 case WCD_MBHC_EN_PULLUP:
144 wcd_mbhc_write_field(mbhc, WCD_MBHC_BTN_ISRC_CTL, 3);
145 wcd_mbhc_write_field(mbhc, WCD_MBHC_FSM_EN, 1);
146 wcd_mbhc_write_field(mbhc, WCD_MBHC_MICB_CTRL, 1);
147 /* Program Button threshold registers as per MICBIAS */
148 wcd_program_btn_threshold(mbhc, true);
150 case WCD_MBHC_EN_NONE:
151 wcd_mbhc_write_field(mbhc, WCD_MBHC_BTN_ISRC_CTL, 0);
152 wcd_mbhc_write_field(mbhc, WCD_MBHC_FSM_EN, 1);
153 wcd_mbhc_write_field(mbhc, WCD_MBHC_MICB_CTRL, 0);
156 dev_err(mbhc->dev, "%s: Invalid parameter", __func__);
161 int wcd_mbhc_event_notify(struct wcd_mbhc *mbhc, unsigned long event)
164 struct snd_soc_component *component;
165 bool micbias2 = false;
170 component = mbhc->component;
172 if (mbhc->mbhc_cb->micbias_enable_status)
173 micbias2 = mbhc->mbhc_cb->micbias_enable_status(component, MIC_BIAS_2);
176 /* MICBIAS usage change */
177 case WCD_EVENT_POST_DAPM_MICBIAS_2_ON:
178 mbhc->is_hs_recording = true;
180 case WCD_EVENT_POST_MICBIAS_2_ON:
181 /* Disable current source if micbias2 enabled */
182 if (mbhc->mbhc_cb->mbhc_micbias_control) {
183 if (wcd_mbhc_read_field(mbhc, WCD_MBHC_FSM_EN))
184 wcd_mbhc_write_field(mbhc, WCD_MBHC_BTN_ISRC_CTL, 0);
186 mbhc->is_hs_recording = true;
187 wcd_mbhc_curr_micbias_control(mbhc, WCD_MBHC_EN_MB);
190 case WCD_EVENT_PRE_MICBIAS_2_OFF:
192 * Before MICBIAS_2 is turned off, if FSM is enabled,
193 * make sure current source is enabled so as to detect
194 * button press/release events
196 if (mbhc->mbhc_cb->mbhc_micbias_control/* && !mbhc->micbias_enable*/) {
197 if (wcd_mbhc_read_field(mbhc, WCD_MBHC_FSM_EN))
198 wcd_mbhc_write_field(mbhc, WCD_MBHC_BTN_ISRC_CTL, 3);
201 /* MICBIAS usage change */
202 case WCD_EVENT_POST_DAPM_MICBIAS_2_OFF:
203 mbhc->is_hs_recording = false;
205 case WCD_EVENT_POST_MICBIAS_2_OFF:
206 if (!mbhc->mbhc_cb->mbhc_micbias_control)
207 mbhc->is_hs_recording = false;
209 /* Enable PULL UP if PA's are enabled */
210 if ((test_bit(WCD_MBHC_EVENT_PA_HPHL, &mbhc->event_state)) ||
211 (test_bit(WCD_MBHC_EVENT_PA_HPHR, &mbhc->event_state)))
212 /* enable pullup and cs, disable mb */
213 wcd_mbhc_curr_micbias_control(mbhc, WCD_MBHC_EN_PULLUP);
215 /* enable current source and disable mb, pullup*/
216 wcd_mbhc_curr_micbias_control(mbhc, WCD_MBHC_EN_CS);
219 case WCD_EVENT_POST_HPHL_PA_OFF:
220 clear_bit(WCD_MBHC_EVENT_PA_HPHL, &mbhc->event_state);
222 /* check if micbias is enabled */
224 /* Disable cs, pullup & enable micbias */
225 wcd_mbhc_curr_micbias_control(mbhc, WCD_MBHC_EN_MB);
227 /* Disable micbias, pullup & enable cs */
228 wcd_mbhc_curr_micbias_control(mbhc, WCD_MBHC_EN_CS);
230 case WCD_EVENT_POST_HPHR_PA_OFF:
231 clear_bit(WCD_MBHC_EVENT_PA_HPHR, &mbhc->event_state);
232 /* check if micbias is enabled */
234 /* Disable cs, pullup & enable micbias */
235 wcd_mbhc_curr_micbias_control(mbhc, WCD_MBHC_EN_MB);
237 /* Disable micbias, pullup & enable cs */
238 wcd_mbhc_curr_micbias_control(mbhc, WCD_MBHC_EN_CS);
240 case WCD_EVENT_PRE_HPHL_PA_ON:
241 set_bit(WCD_MBHC_EVENT_PA_HPHL, &mbhc->event_state);
242 /* check if micbias is enabled */
244 /* Disable cs, pullup & enable micbias */
245 wcd_mbhc_curr_micbias_control(mbhc, WCD_MBHC_EN_MB);
247 /* Disable micbias, enable pullup & cs */
248 wcd_mbhc_curr_micbias_control(mbhc, WCD_MBHC_EN_PULLUP);
250 case WCD_EVENT_PRE_HPHR_PA_ON:
251 set_bit(WCD_MBHC_EVENT_PA_HPHR, &mbhc->event_state);
252 /* check if micbias is enabled */
254 /* Disable cs, pullup & enable micbias */
255 wcd_mbhc_curr_micbias_control(mbhc, WCD_MBHC_EN_MB);
257 /* Disable micbias, enable pullup & cs */
258 wcd_mbhc_curr_micbias_control(mbhc, WCD_MBHC_EN_PULLUP);
265 EXPORT_SYMBOL_GPL(wcd_mbhc_event_notify);
267 static int wcd_cancel_btn_work(struct wcd_mbhc *mbhc)
269 return cancel_delayed_work_sync(&mbhc->mbhc_btn_dwork);
272 static void wcd_micbias_disable(struct wcd_mbhc *mbhc)
274 struct snd_soc_component *component = mbhc->component;
276 if (mbhc->mbhc_cb->mbhc_micbias_control)
277 mbhc->mbhc_cb->mbhc_micbias_control(component, MIC_BIAS_2, MICB_DISABLE);
279 if (mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic)
280 mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic(component, MIC_BIAS_2, false);
282 if (mbhc->mbhc_cb->set_micbias_value) {
283 mbhc->mbhc_cb->set_micbias_value(component);
284 wcd_mbhc_write_field(mbhc, WCD_MBHC_MICB_CTRL, 0);
288 static void wcd_mbhc_report_plug_removal(struct wcd_mbhc *mbhc,
289 enum snd_jack_types jack_type)
291 mbhc->hph_status &= ~jack_type;
293 * cancel possibly scheduled btn work and
294 * report release if we reported button press
296 if (!wcd_cancel_btn_work(mbhc) && mbhc->buttons_pressed) {
297 snd_soc_jack_report(mbhc->jack, 0, mbhc->buttons_pressed);
298 mbhc->buttons_pressed &= ~WCD_MBHC_JACK_BUTTON_MASK;
301 wcd_micbias_disable(mbhc);
302 mbhc->hph_type = WCD_MBHC_HPH_NONE;
303 mbhc->zl = mbhc->zr = 0;
304 snd_soc_jack_report(mbhc->jack, mbhc->hph_status, WCD_MBHC_JACK_MASK);
305 mbhc->current_plug = MBHC_PLUG_TYPE_NONE;
306 mbhc->force_linein = false;
309 static void wcd_mbhc_compute_impedance(struct wcd_mbhc *mbhc)
312 if (!mbhc->impedance_detect)
315 if (mbhc->cfg->linein_th != 0) {
316 u8 fsm_en = wcd_mbhc_read_field(mbhc, WCD_MBHC_FSM_EN);
317 /* Set MUX_CTL to AUTO for Z-det */
319 wcd_mbhc_write_field(mbhc, WCD_MBHC_FSM_EN, 0);
320 wcd_mbhc_write_field(mbhc, WCD_MBHC_MUX_CTL, MUX_CTL_AUTO);
321 wcd_mbhc_write_field(mbhc, WCD_MBHC_FSM_EN, 1);
322 mbhc->mbhc_cb->compute_impedance(mbhc->component, &mbhc->zl, &mbhc->zr);
323 wcd_mbhc_write_field(mbhc, WCD_MBHC_FSM_EN, fsm_en);
327 static void wcd_mbhc_report_plug_insertion(struct wcd_mbhc *mbhc,
328 enum snd_jack_types jack_type)
332 * Report removal of current jack type.
333 * Headphone to headset shouldn't report headphone
336 if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADSET &&
337 jack_type == SND_JACK_HEADPHONE)
338 mbhc->hph_status &= ~SND_JACK_HEADSET;
340 /* Report insertion */
342 case SND_JACK_HEADPHONE:
343 mbhc->current_plug = MBHC_PLUG_TYPE_HEADPHONE;
345 case SND_JACK_HEADSET:
346 mbhc->current_plug = MBHC_PLUG_TYPE_HEADSET;
347 mbhc->jiffies_atreport = jiffies;
349 case SND_JACK_LINEOUT:
350 mbhc->current_plug = MBHC_PLUG_TYPE_HIGH_HPH;
357 is_pa_on = wcd_mbhc_read_field(mbhc, WCD_MBHC_HPH_PA_EN);
360 wcd_mbhc_compute_impedance(mbhc);
361 if ((mbhc->zl > mbhc->cfg->linein_th) &&
362 (mbhc->zr > mbhc->cfg->linein_th) &&
363 (jack_type == SND_JACK_HEADPHONE)) {
364 jack_type = SND_JACK_LINEOUT;
365 mbhc->force_linein = true;
366 mbhc->current_plug = MBHC_PLUG_TYPE_HIGH_HPH;
367 if (mbhc->hph_status) {
368 mbhc->hph_status &= ~(SND_JACK_HEADSET |
370 snd_soc_jack_report(mbhc->jack, mbhc->hph_status,
376 /* Do not calculate impedance again for lineout
377 * as during playback pa is on and impedance values
378 * will not be correct resulting in lineout detected
381 if (is_pa_on && mbhc->force_linein) {
382 jack_type = SND_JACK_LINEOUT;
383 mbhc->current_plug = MBHC_PLUG_TYPE_HIGH_HPH;
384 if (mbhc->hph_status) {
385 mbhc->hph_status &= ~(SND_JACK_HEADSET |
387 snd_soc_jack_report(mbhc->jack, mbhc->hph_status,
392 mbhc->hph_status |= jack_type;
394 if (jack_type == SND_JACK_HEADPHONE && mbhc->mbhc_cb->mbhc_micb_ramp_control)
395 mbhc->mbhc_cb->mbhc_micb_ramp_control(mbhc->component, false);
397 snd_soc_jack_report(mbhc->jack, (mbhc->hph_status | SND_JACK_MECHANICAL),
401 static void wcd_mbhc_report_plug(struct wcd_mbhc *mbhc, int insertion,
402 enum snd_jack_types jack_type)
405 WARN_ON(!mutex_is_locked(&mbhc->lock));
407 if (!insertion) /* Report removal */
408 wcd_mbhc_report_plug_removal(mbhc, jack_type);
410 wcd_mbhc_report_plug_insertion(mbhc, jack_type);
414 static void wcd_cancel_hs_detect_plug(struct wcd_mbhc *mbhc,
415 struct work_struct *work)
417 mbhc->hs_detect_work_stop = true;
418 mutex_unlock(&mbhc->lock);
419 cancel_work_sync(work);
420 mutex_lock(&mbhc->lock);
423 static void wcd_mbhc_cancel_pending_work(struct wcd_mbhc *mbhc)
425 /* cancel pending button press */
426 wcd_cancel_btn_work(mbhc);
427 /* cancel correct work function */
428 wcd_cancel_hs_detect_plug(mbhc, &mbhc->correct_plug_swch);
431 static void wcd_mbhc_elec_hs_report_unplug(struct wcd_mbhc *mbhc)
433 wcd_mbhc_cancel_pending_work(mbhc);
434 /* Report extension cable */
435 wcd_mbhc_report_plug(mbhc, 1, SND_JACK_LINEOUT);
437 * Disable HPHL trigger and MIC Schmitt triggers.
438 * Setup for insertion detection.
440 disable_irq_nosync(mbhc->intr_ids->mbhc_hs_rem_intr);
441 wcd_mbhc_curr_micbias_control(mbhc, WCD_MBHC_EN_NONE);
443 wcd_mbhc_write_field(mbhc, WCD_MBHC_FSM_EN, 0);
444 wcd_mbhc_write_field(mbhc, WCD_MBHC_ELECT_SCHMT_ISRC, 3);
446 /* Set the detection type appropriately */
447 wcd_mbhc_write_field(mbhc, WCD_MBHC_ELECT_DETECTION_TYPE, 1);
448 enable_irq(mbhc->intr_ids->mbhc_hs_ins_intr);
451 static void wcd_mbhc_find_plug_and_report(struct wcd_mbhc *mbhc,
452 enum wcd_mbhc_plug_type plug_type)
454 if (mbhc->current_plug == plug_type)
457 mutex_lock(&mbhc->lock);
460 case MBHC_PLUG_TYPE_HEADPHONE:
461 wcd_mbhc_report_plug(mbhc, 1, SND_JACK_HEADPHONE);
463 case MBHC_PLUG_TYPE_HEADSET:
464 wcd_mbhc_report_plug(mbhc, 1, SND_JACK_HEADSET);
466 case MBHC_PLUG_TYPE_HIGH_HPH:
467 wcd_mbhc_report_plug(mbhc, 1, SND_JACK_LINEOUT);
469 case MBHC_PLUG_TYPE_GND_MIC_SWAP:
470 if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADPHONE)
471 wcd_mbhc_report_plug(mbhc, 0, SND_JACK_HEADPHONE);
472 if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADSET)
473 wcd_mbhc_report_plug(mbhc, 0, SND_JACK_HEADSET);
476 WARN(1, "Unexpected current plug_type %d, plug_type %d\n",
477 mbhc->current_plug, plug_type);
480 mutex_unlock(&mbhc->lock);
483 static void wcd_schedule_hs_detect_plug(struct wcd_mbhc *mbhc,
484 struct work_struct *work)
486 WARN_ON(!mutex_is_locked(&mbhc->lock));
487 mbhc->hs_detect_work_stop = false;
491 static void wcd_mbhc_adc_detect_plug_type(struct wcd_mbhc *mbhc)
493 struct snd_soc_component *component = mbhc->component;
495 WARN_ON(!mutex_is_locked(&mbhc->lock));
497 if (mbhc->mbhc_cb->hph_pull_down_ctrl)
498 mbhc->mbhc_cb->hph_pull_down_ctrl(component, false);
500 wcd_mbhc_write_field(mbhc, WCD_MBHC_DETECTION_DONE, 0);
502 if (mbhc->mbhc_cb->mbhc_micbias_control) {
503 mbhc->mbhc_cb->mbhc_micbias_control(component, MIC_BIAS_2,
505 wcd_schedule_hs_detect_plug(mbhc, &mbhc->correct_plug_swch);
509 static irqreturn_t wcd_mbhc_mech_plug_detect_irq(int irq, void *data)
511 struct snd_soc_component *component;
512 enum snd_jack_types jack_type;
513 struct wcd_mbhc *mbhc = data;
516 component = mbhc->component;
517 mutex_lock(&mbhc->lock);
519 mbhc->in_swch_irq_handler = true;
521 wcd_mbhc_cancel_pending_work(mbhc);
523 detection_type = wcd_mbhc_read_field(mbhc, WCD_MBHC_MECH_DETECTION_TYPE);
525 /* Set the detection type appropriately */
526 wcd_mbhc_write_field(mbhc, WCD_MBHC_MECH_DETECTION_TYPE, !detection_type);
528 /* Enable micbias ramp */
529 if (mbhc->mbhc_cb->mbhc_micb_ramp_control)
530 mbhc->mbhc_cb->mbhc_micb_ramp_control(component, true);
532 if (detection_type) {
533 if (mbhc->current_plug != MBHC_PLUG_TYPE_NONE)
535 /* Make sure MASTER_BIAS_CTL is enabled */
536 mbhc->mbhc_cb->mbhc_bias(component, true);
537 mbhc->is_btn_press = false;
538 wcd_mbhc_adc_detect_plug_type(mbhc);
541 wcd_mbhc_write_field(mbhc, WCD_MBHC_FSM_EN, 0);
542 wcd_mbhc_write_field(mbhc, WCD_MBHC_BTN_ISRC_CTL, 0);
543 mbhc->extn_cable_hph_rem = false;
545 if (mbhc->current_plug == MBHC_PLUG_TYPE_NONE)
548 mbhc->is_btn_press = false;
549 switch (mbhc->current_plug) {
550 case MBHC_PLUG_TYPE_HEADPHONE:
551 jack_type = SND_JACK_HEADPHONE;
553 case MBHC_PLUG_TYPE_HEADSET:
554 jack_type = SND_JACK_HEADSET;
556 case MBHC_PLUG_TYPE_HIGH_HPH:
557 if (mbhc->mbhc_detection_logic == WCD_DETECTION_ADC)
558 wcd_mbhc_write_field(mbhc, WCD_MBHC_ELECT_ISRC_EN, 0);
559 jack_type = SND_JACK_LINEOUT;
561 case MBHC_PLUG_TYPE_GND_MIC_SWAP:
562 dev_err(mbhc->dev, "Ground and Mic Swapped on plug\n");
565 dev_err(mbhc->dev, "Invalid current plug: %d\n",
569 disable_irq_nosync(mbhc->intr_ids->mbhc_hs_rem_intr);
570 disable_irq_nosync(mbhc->intr_ids->mbhc_hs_ins_intr);
571 wcd_mbhc_write_field(mbhc, WCD_MBHC_ELECT_DETECTION_TYPE, 1);
572 wcd_mbhc_write_field(mbhc, WCD_MBHC_ELECT_SCHMT_ISRC, 0);
573 wcd_mbhc_report_plug(mbhc, 0, jack_type);
577 mbhc->in_swch_irq_handler = false;
578 mutex_unlock(&mbhc->lock);
582 static int wcd_mbhc_get_button_mask(struct wcd_mbhc *mbhc)
587 btn = wcd_mbhc_read_field(mbhc, WCD_MBHC_BTN_RESULT);
591 mask = SND_JACK_BTN_0;
594 mask = SND_JACK_BTN_1;
597 mask = SND_JACK_BTN_2;
600 mask = SND_JACK_BTN_3;
603 mask = SND_JACK_BTN_4;
606 mask = SND_JACK_BTN_5;
615 static void wcd_btn_long_press_fn(struct work_struct *work)
617 struct delayed_work *dwork = to_delayed_work(work);
618 struct wcd_mbhc *mbhc = container_of(dwork, struct wcd_mbhc, mbhc_btn_dwork);
620 if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADSET)
621 snd_soc_jack_report(mbhc->jack, mbhc->buttons_pressed,
622 mbhc->buttons_pressed);
625 static irqreturn_t wcd_mbhc_btn_press_handler(int irq, void *data)
627 struct wcd_mbhc *mbhc = data;
629 unsigned long msec_val;
631 mutex_lock(&mbhc->lock);
632 wcd_cancel_btn_work(mbhc);
633 mbhc->is_btn_press = true;
634 msec_val = jiffies_to_msecs(jiffies - mbhc->jiffies_atreport);
636 /* Too short, ignore button press */
637 if (msec_val < MBHC_BUTTON_PRESS_THRESHOLD_MIN)
640 /* If switch interrupt already kicked in, ignore button press */
641 if (mbhc->in_swch_irq_handler)
644 /* Plug isn't headset, ignore button press */
645 if (mbhc->current_plug != MBHC_PLUG_TYPE_HEADSET)
648 mask = wcd_mbhc_get_button_mask(mbhc);
649 mbhc->buttons_pressed |= mask;
650 if (schedule_delayed_work(&mbhc->mbhc_btn_dwork, msecs_to_jiffies(400)) == 0)
651 WARN(1, "Button pressed twice without release event\n");
653 mutex_unlock(&mbhc->lock);
657 static irqreturn_t wcd_mbhc_btn_release_handler(int irq, void *data)
659 struct wcd_mbhc *mbhc = data;
662 mutex_lock(&mbhc->lock);
663 if (mbhc->is_btn_press)
664 mbhc->is_btn_press = false;
665 else /* fake btn press */
668 if (!(mbhc->buttons_pressed & WCD_MBHC_JACK_BUTTON_MASK))
671 ret = wcd_cancel_btn_work(mbhc);
672 if (ret == 0) { /* Reporting long button release event */
673 snd_soc_jack_report(mbhc->jack, 0, mbhc->buttons_pressed);
675 if (!mbhc->in_swch_irq_handler) {
676 /* Reporting btn press n Release */
677 snd_soc_jack_report(mbhc->jack, mbhc->buttons_pressed,
678 mbhc->buttons_pressed);
679 snd_soc_jack_report(mbhc->jack, 0, mbhc->buttons_pressed);
682 mbhc->buttons_pressed &= ~WCD_MBHC_JACK_BUTTON_MASK;
684 mutex_unlock(&mbhc->lock);
689 static irqreturn_t wcd_mbhc_hph_ocp_irq(struct wcd_mbhc *mbhc, bool hphr)
692 /* TODO Find a better way to report this to Userspace */
693 dev_err(mbhc->dev, "MBHC Over Current on %s detected\n",
694 hphr ? "HPHR" : "HPHL");
696 wcd_mbhc_write_field(mbhc, WCD_MBHC_OCP_FSM_EN, 0);
697 wcd_mbhc_write_field(mbhc, WCD_MBHC_OCP_FSM_EN, 1);
702 static irqreturn_t wcd_mbhc_hphl_ocp_irq(int irq, void *data)
704 return wcd_mbhc_hph_ocp_irq(data, false);
707 static irqreturn_t wcd_mbhc_hphr_ocp_irq(int irq, void *data)
709 return wcd_mbhc_hph_ocp_irq(data, true);
712 static int wcd_mbhc_initialise(struct wcd_mbhc *mbhc)
714 struct snd_soc_component *component = mbhc->component;
717 ret = pm_runtime_get_sync(component->dev);
718 if (ret < 0 && ret != -EACCES) {
719 dev_err_ratelimited(component->dev,
720 "pm_runtime_get_sync failed in %s, ret %d\n",
722 pm_runtime_put_noidle(component->dev);
726 mutex_lock(&mbhc->lock);
728 /* enable HS detection */
729 if (mbhc->mbhc_cb->hph_pull_up_control_v2)
730 mbhc->mbhc_cb->hph_pull_up_control_v2(component,
731 HS_PULLUP_I_DEFAULT);
732 else if (mbhc->mbhc_cb->hph_pull_up_control)
733 mbhc->mbhc_cb->hph_pull_up_control(component, I_DEFAULT);
735 wcd_mbhc_write_field(mbhc, WCD_MBHC_HS_L_DET_PULL_UP_CTRL, 3);
737 wcd_mbhc_write_field(mbhc, WCD_MBHC_HPHL_PLUG_TYPE, mbhc->cfg->hphl_swh);
738 wcd_mbhc_write_field(mbhc, WCD_MBHC_GND_PLUG_TYPE, mbhc->cfg->gnd_swh);
739 wcd_mbhc_write_field(mbhc, WCD_MBHC_SW_HPH_LP_100K_TO_GND, 1);
740 if (mbhc->cfg->gnd_det_en && mbhc->mbhc_cb->mbhc_gnd_det_ctrl)
741 mbhc->mbhc_cb->mbhc_gnd_det_ctrl(component, true);
742 wcd_mbhc_write_field(mbhc, WCD_MBHC_HS_L_DET_PULL_UP_COMP_CTRL, 1);
744 wcd_mbhc_write_field(mbhc, WCD_MBHC_L_DET_EN, 1);
746 /* Insertion debounce set to 96ms */
747 wcd_mbhc_write_field(mbhc, WCD_MBHC_INSREM_DBNC, 6);
749 /* Button Debounce set to 16ms */
750 wcd_mbhc_write_field(mbhc, WCD_MBHC_BTN_DBNC, 2);
753 mbhc->mbhc_cb->mbhc_bias(component, true);
754 /* enable MBHC clock */
755 if (mbhc->mbhc_cb->clk_setup)
756 mbhc->mbhc_cb->clk_setup(component, true);
758 /* program HS_VREF value */
759 wcd_program_hs_vref(mbhc);
761 wcd_program_btn_threshold(mbhc, false);
763 mutex_unlock(&mbhc->lock);
765 pm_runtime_mark_last_busy(component->dev);
766 pm_runtime_put_autosuspend(component->dev);
771 static int wcd_mbhc_get_micbias(struct wcd_mbhc *mbhc)
775 if (mbhc->mbhc_cb->get_micbias_val) {
776 mbhc->mbhc_cb->get_micbias_val(mbhc->component, &micbias);
779 /* Read MBHC Micbias (Mic Bias2) voltage */
780 vout_ctl = wcd_mbhc_read_field(mbhc, WCD_MBHC_MICB2_VOUT);
781 /* Formula for getting micbias from vout
782 * micbias = 1.0V + VOUT_CTL * 50mV
784 micbias = 1000 + (vout_ctl * 50);
789 static int wcd_get_voltage_from_adc(u8 val, int micbias)
791 /* Formula for calculating voltage from ADC
792 * Voltage = ADC_RESULT*12.5mV*V_MICBIAS/1.8
794 return ((val * 125 * micbias)/(WCD_MBHC_ADC_MICBIAS_MV * 10));
797 static int wcd_measure_adc_continuous(struct wcd_mbhc *mbhc)
804 /* Pre-requisites for ADC continuous measurement */
805 /* Read legacy electircal detection and disable */
806 wcd_mbhc_write_field(mbhc, WCD_MBHC_ELECT_SCHMT_ISRC, 0x00);
807 /* Set ADC to continuous measurement */
808 wcd_mbhc_write_field(mbhc, WCD_MBHC_ADC_MODE, 1);
809 /* Read ADC Enable bit to restore after adc measurement */
810 adc_en = wcd_mbhc_read_field(mbhc, WCD_MBHC_ADC_EN);
811 /* Disable ADC_ENABLE bit */
812 wcd_mbhc_write_field(mbhc, WCD_MBHC_ADC_EN, 0);
813 /* Disable MBHC FSM */
814 wcd_mbhc_write_field(mbhc, WCD_MBHC_FSM_EN, 0);
815 /* Set the MUX selection to IN2P */
816 wcd_mbhc_write_field(mbhc, WCD_MBHC_MUX_CTL, MUX_CTL_IN2P);
817 /* Enable MBHC FSM */
818 wcd_mbhc_write_field(mbhc, WCD_MBHC_FSM_EN, 1);
819 /* Enable ADC_ENABLE bit */
820 wcd_mbhc_write_field(mbhc, WCD_MBHC_ADC_EN, 1);
823 /* wait for 3 msec before reading ADC result */
824 usleep_range(3000, 3100);
825 adc_result = wcd_mbhc_read_field(mbhc, WCD_MBHC_ADC_RESULT);
828 /* Restore ADC Enable */
829 wcd_mbhc_write_field(mbhc, WCD_MBHC_ADC_EN, adc_en);
830 /* Get voltage from ADC result */
831 output_mv = wcd_get_voltage_from_adc(adc_result, wcd_mbhc_get_micbias(mbhc));
836 static int wcd_measure_adc_once(struct wcd_mbhc *mbhc, int mux_ctl)
838 struct device *dev = mbhc->dev;
847 wcd_mbhc_write_field(mbhc, WCD_MBHC_ADC_MODE, 0);
848 /* Read ADC Enable bit to restore after adc measurement */
849 adc_en = wcd_mbhc_read_field(mbhc, WCD_MBHC_ADC_EN);
850 /* Trigger ADC one time measurement */
851 wcd_mbhc_write_field(mbhc, WCD_MBHC_ADC_EN, 0);
852 wcd_mbhc_write_field(mbhc, WCD_MBHC_FSM_EN, 0);
853 /* Set the appropriate MUX selection */
854 wcd_mbhc_write_field(mbhc, WCD_MBHC_MUX_CTL, mux_ctl);
855 wcd_mbhc_write_field(mbhc, WCD_MBHC_FSM_EN, 1);
856 wcd_mbhc_write_field(mbhc, WCD_MBHC_ADC_EN, 1);
859 /* wait for 600usec to get adc results */
860 usleep_range(600, 610);
862 /* check for ADC Timeout */
863 adc_timeout = wcd_mbhc_read_field(mbhc, WCD_MBHC_ADC_TIMEOUT);
867 /* Read ADC complete bit */
868 adc_complete = wcd_mbhc_read_field(mbhc, WCD_MBHC_ADC_COMPLETE);
872 /* Read ADC result */
873 adc_result = wcd_mbhc_read_field(mbhc, WCD_MBHC_ADC_RESULT);
875 /* Get voltage from ADC result */
876 output_mv = wcd_get_voltage_from_adc(adc_result,
877 wcd_mbhc_get_micbias(mbhc));
881 /* Restore ADC Enable */
882 wcd_mbhc_write_field(mbhc, WCD_MBHC_ADC_EN, adc_en);
885 dev_err(dev, "%s: adc complete: %d, adc timeout: %d\n",
886 __func__, adc_complete, adc_timeout);
895 /* To determine if cross connection occurred */
896 static int wcd_check_cross_conn(struct wcd_mbhc *mbhc)
898 u8 adc_mode, elect_ctl, adc_en, fsm_en;
899 int hphl_adc_res, hphr_adc_res;
900 bool is_cross_conn = false;
902 /* If PA is enabled, dont check for cross-connection */
903 if (wcd_mbhc_read_field(mbhc, WCD_MBHC_HPH_PA_EN))
906 /* Read legacy electircal detection and disable */
907 elect_ctl = wcd_mbhc_read_field(mbhc, WCD_MBHC_ELECT_SCHMT_ISRC);
908 wcd_mbhc_write_field(mbhc, WCD_MBHC_ELECT_SCHMT_ISRC, 0);
910 /* Read and set ADC to single measurement */
911 adc_mode = wcd_mbhc_read_field(mbhc, WCD_MBHC_ADC_MODE);
912 /* Read ADC Enable bit to restore after adc measurement */
913 adc_en = wcd_mbhc_read_field(mbhc, WCD_MBHC_ADC_EN);
914 /* Read FSM status */
915 fsm_en = wcd_mbhc_read_field(mbhc, WCD_MBHC_FSM_EN);
917 /* Get adc result for HPH L */
918 hphl_adc_res = wcd_measure_adc_once(mbhc, MUX_CTL_HPH_L);
919 if (hphl_adc_res < 0)
922 /* Get adc result for HPH R in mV */
923 hphr_adc_res = wcd_measure_adc_once(mbhc, MUX_CTL_HPH_R);
924 if (hphr_adc_res < 0)
927 if (hphl_adc_res > HPHL_CROSS_CONN_THRESHOLD ||
928 hphr_adc_res > HPHL_CROSS_CONN_THRESHOLD)
929 is_cross_conn = true;
931 wcd_mbhc_write_field(mbhc, WCD_MBHC_FSM_EN, 0);
932 /* Set the MUX selection to Auto */
933 wcd_mbhc_write_field(mbhc, WCD_MBHC_MUX_CTL, MUX_CTL_AUTO);
934 wcd_mbhc_write_field(mbhc, WCD_MBHC_FSM_EN, 1);
935 /* Restore ADC Enable */
936 wcd_mbhc_write_field(mbhc, WCD_MBHC_ADC_EN, adc_en);
937 /* Restore ADC mode */
938 wcd_mbhc_write_field(mbhc, WCD_MBHC_ADC_MODE, adc_mode);
939 /* Restore FSM state */
940 wcd_mbhc_write_field(mbhc, WCD_MBHC_FSM_EN, fsm_en);
941 /* Restore electrical detection */
942 wcd_mbhc_write_field(mbhc, WCD_MBHC_ELECT_SCHMT_ISRC, elect_ctl);
944 return is_cross_conn;
947 static int wcd_mbhc_adc_get_hs_thres(struct wcd_mbhc *mbhc)
949 int hs_threshold, micbias_mv;
951 micbias_mv = wcd_mbhc_get_micbias(mbhc);
952 if (mbhc->cfg->hs_thr) {
953 if (mbhc->cfg->micb_mv == micbias_mv)
954 hs_threshold = mbhc->cfg->hs_thr;
956 hs_threshold = (mbhc->cfg->hs_thr *
957 micbias_mv) / mbhc->cfg->micb_mv;
959 hs_threshold = ((WCD_MBHC_ADC_HS_THRESHOLD_MV *
960 micbias_mv) / WCD_MBHC_ADC_MICBIAS_MV);
965 static int wcd_mbhc_adc_get_hph_thres(struct wcd_mbhc *mbhc)
967 int hph_threshold, micbias_mv;
969 micbias_mv = wcd_mbhc_get_micbias(mbhc);
970 if (mbhc->cfg->hph_thr) {
971 if (mbhc->cfg->micb_mv == micbias_mv)
972 hph_threshold = mbhc->cfg->hph_thr;
974 hph_threshold = (mbhc->cfg->hph_thr *
975 micbias_mv) / mbhc->cfg->micb_mv;
977 hph_threshold = ((WCD_MBHC_ADC_HPH_THRESHOLD_MV *
978 micbias_mv) / WCD_MBHC_ADC_MICBIAS_MV);
980 return hph_threshold;
983 static void wcd_mbhc_adc_update_fsm_source(struct wcd_mbhc *mbhc,
984 enum wcd_mbhc_plug_type plug_type)
986 bool micbias2 = false;
989 case MBHC_PLUG_TYPE_HEADPHONE:
990 wcd_mbhc_write_field(mbhc, WCD_MBHC_BTN_ISRC_CTL, 3);
992 case MBHC_PLUG_TYPE_HEADSET:
993 if (mbhc->mbhc_cb->micbias_enable_status)
994 micbias2 = mbhc->mbhc_cb->micbias_enable_status(mbhc->component,
997 if (!mbhc->is_hs_recording && !micbias2)
998 wcd_mbhc_write_field(mbhc, WCD_MBHC_BTN_ISRC_CTL, 3);
1001 wcd_mbhc_write_field(mbhc, WCD_MBHC_BTN_ISRC_CTL, 0);
1007 static void wcd_mbhc_bcs_enable(struct wcd_mbhc *mbhc, int plug_type, bool enable)
1009 switch (plug_type) {
1010 case MBHC_PLUG_TYPE_HEADSET:
1011 case MBHC_PLUG_TYPE_HEADPHONE:
1012 if (mbhc->mbhc_cb->bcs_enable)
1013 mbhc->mbhc_cb->bcs_enable(mbhc->component, enable);
1020 static int wcd_mbhc_get_plug_from_adc(struct wcd_mbhc *mbhc, int adc_result)
1023 enum wcd_mbhc_plug_type plug_type;
1024 u32 hph_thr, hs_thr;
1026 hs_thr = wcd_mbhc_adc_get_hs_thres(mbhc);
1027 hph_thr = wcd_mbhc_adc_get_hph_thres(mbhc);
1029 if (adc_result < hph_thr)
1030 plug_type = MBHC_PLUG_TYPE_HEADPHONE;
1031 else if (adc_result > hs_thr)
1032 plug_type = MBHC_PLUG_TYPE_HIGH_HPH;
1034 plug_type = MBHC_PLUG_TYPE_HEADSET;
1039 static int wcd_mbhc_get_spl_hs_thres(struct wcd_mbhc *mbhc)
1041 int hs_threshold, micbias_mv;
1043 micbias_mv = wcd_mbhc_get_micbias(mbhc);
1044 if (mbhc->cfg->hs_thr && mbhc->cfg->micb_mv != WCD_MBHC_ADC_MICBIAS_MV) {
1045 if (mbhc->cfg->micb_mv == micbias_mv)
1046 hs_threshold = mbhc->cfg->hs_thr;
1048 hs_threshold = (mbhc->cfg->hs_thr * micbias_mv) / mbhc->cfg->micb_mv;
1050 hs_threshold = ((WCD_MBHC_ADC_HS_THRESHOLD_MV * micbias_mv) /
1051 WCD_MBHC_ADC_MICBIAS_MV);
1053 return hs_threshold;
1056 static bool wcd_mbhc_check_for_spl_headset(struct wcd_mbhc *mbhc)
1058 bool is_spl_hs = false;
1059 int output_mv, hs_threshold, hph_threshold;
1061 if (!mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic)
1064 /* Bump up MIC_BIAS2 to 2.7V */
1065 mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic(mbhc->component, MIC_BIAS_2, true);
1066 usleep_range(10000, 10100);
1068 output_mv = wcd_measure_adc_once(mbhc, MUX_CTL_IN2P);
1069 hs_threshold = wcd_mbhc_get_spl_hs_thres(mbhc);
1070 hph_threshold = wcd_mbhc_adc_get_hph_thres(mbhc);
1072 if (!(output_mv > hs_threshold || output_mv < hph_threshold))
1075 /* Back MIC_BIAS2 to 1.8v if the type is not special headset */
1077 mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic(mbhc->component, MIC_BIAS_2, false);
1078 /* Add 10ms delay for micbias to settle */
1079 usleep_range(10000, 10100);
1085 static void wcd_correct_swch_plug(struct work_struct *work)
1087 struct wcd_mbhc *mbhc;
1088 struct snd_soc_component *component;
1089 enum wcd_mbhc_plug_type plug_type = MBHC_PLUG_TYPE_INVALID;
1090 unsigned long timeout;
1091 int pt_gnd_mic_swap_cnt = 0;
1092 int output_mv, cross_conn, hs_threshold, try = 0, micbias_mv;
1093 bool is_spl_hs = false;
1097 mbhc = container_of(work, struct wcd_mbhc, correct_plug_swch);
1098 component = mbhc->component;
1100 ret = pm_runtime_get_sync(component->dev);
1101 if (ret < 0 && ret != -EACCES) {
1102 dev_err_ratelimited(component->dev,
1103 "pm_runtime_get_sync failed in %s, ret %d\n",
1105 pm_runtime_put_noidle(component->dev);
1108 micbias_mv = wcd_mbhc_get_micbias(mbhc);
1109 hs_threshold = wcd_mbhc_adc_get_hs_thres(mbhc);
1111 /* Mask ADC COMPLETE interrupt */
1112 disable_irq_nosync(mbhc->intr_ids->mbhc_hs_ins_intr);
1114 /* Check for cross connection */
1116 cross_conn = wcd_check_cross_conn(mbhc);
1118 } while (try < GND_MIC_SWAP_THRESHOLD);
1120 if (cross_conn > 0) {
1121 plug_type = MBHC_PLUG_TYPE_GND_MIC_SWAP;
1122 dev_err(mbhc->dev, "cross connection found, Plug type %d\n",
1124 goto correct_plug_type;
1127 /* Find plug type */
1128 output_mv = wcd_measure_adc_continuous(mbhc);
1129 plug_type = wcd_mbhc_get_plug_from_adc(mbhc, output_mv);
1132 * Report plug type if it is either headset or headphone
1133 * else start the 3 sec loop
1135 switch (plug_type) {
1136 case MBHC_PLUG_TYPE_HEADPHONE:
1137 wcd_mbhc_find_plug_and_report(mbhc, plug_type);
1139 case MBHC_PLUG_TYPE_HEADSET:
1140 wcd_mbhc_find_plug_and_report(mbhc, plug_type);
1141 wcd_mbhc_write_field(mbhc, WCD_MBHC_ADC_MODE, 0);
1142 wcd_mbhc_write_field(mbhc, WCD_MBHC_ADC_EN, 0);
1143 wcd_mbhc_write_field(mbhc, WCD_MBHC_DETECTION_DONE, 1);
1151 /* Disable BCS slow insertion detection */
1152 wcd_mbhc_bcs_enable(mbhc, plug_type, false);
1154 timeout = jiffies + msecs_to_jiffies(HS_DETECT_PLUG_TIME_MS);
1156 while (!time_after(jiffies, timeout)) {
1157 if (mbhc->hs_detect_work_stop) {
1158 wcd_micbias_disable(mbhc);
1164 * Use ADC single mode to minimize the chance of missing out
1165 * btn press/release for HEADSET type during correct work.
1167 output_mv = wcd_measure_adc_once(mbhc, MUX_CTL_IN2P);
1168 plug_type = wcd_mbhc_get_plug_from_adc(mbhc, output_mv);
1169 is_pa_on = wcd_mbhc_read_field(mbhc, WCD_MBHC_HPH_PA_EN);
1171 if (output_mv > hs_threshold && !is_spl_hs) {
1172 is_spl_hs = wcd_mbhc_check_for_spl_headset(mbhc);
1173 output_mv = wcd_measure_adc_once(mbhc, MUX_CTL_IN2P);
1176 hs_threshold *= wcd_mbhc_get_micbias(mbhc);
1177 hs_threshold /= micbias_mv;
1181 if ((output_mv <= hs_threshold) && !is_pa_on) {
1182 /* Check for cross connection*/
1183 cross_conn = wcd_check_cross_conn(mbhc);
1184 if (cross_conn > 0) { /* cross-connection */
1185 pt_gnd_mic_swap_cnt++;
1186 if (pt_gnd_mic_swap_cnt < GND_MIC_SWAP_THRESHOLD)
1189 plug_type = MBHC_PLUG_TYPE_GND_MIC_SWAP;
1190 } else if (!cross_conn) { /* no cross connection */
1191 pt_gnd_mic_swap_cnt = 0;
1192 plug_type = wcd_mbhc_get_plug_from_adc(mbhc, output_mv);
1194 } else /* Error if (cross_conn < 0) */
1197 if (pt_gnd_mic_swap_cnt == GND_MIC_SWAP_THRESHOLD) {
1198 /* US_EU gpio present, flip switch */
1199 if (mbhc->cfg->swap_gnd_mic) {
1200 if (mbhc->cfg->swap_gnd_mic(component, true))
1206 /* cable is extension cable */
1207 if (output_mv > hs_threshold || mbhc->force_linein)
1208 plug_type = MBHC_PLUG_TYPE_HIGH_HPH;
1211 wcd_mbhc_bcs_enable(mbhc, plug_type, true);
1213 if (plug_type == MBHC_PLUG_TYPE_HIGH_HPH) {
1215 plug_type = MBHC_PLUG_TYPE_HEADSET;
1217 wcd_mbhc_write_field(mbhc, WCD_MBHC_ELECT_ISRC_EN, 1);
1220 wcd_mbhc_write_field(mbhc, WCD_MBHC_ADC_MODE, 0);
1221 wcd_mbhc_write_field(mbhc, WCD_MBHC_ADC_EN, 0);
1222 wcd_mbhc_find_plug_and_report(mbhc, plug_type);
1225 * Set DETECTION_DONE bit for HEADSET
1226 * so that btn press/release interrupt can be generated.
1227 * For other plug type, clear the bit.
1229 if (plug_type == MBHC_PLUG_TYPE_HEADSET)
1230 wcd_mbhc_write_field(mbhc, WCD_MBHC_DETECTION_DONE, 1);
1232 wcd_mbhc_write_field(mbhc, WCD_MBHC_DETECTION_DONE, 0);
1234 if (mbhc->mbhc_cb->mbhc_micbias_control)
1235 wcd_mbhc_adc_update_fsm_source(mbhc, plug_type);
1238 if (mbhc->mbhc_cb->mbhc_micbias_control/* && !mbhc->micbias_enable*/)
1239 mbhc->mbhc_cb->mbhc_micbias_control(component, MIC_BIAS_2, MICB_DISABLE);
1242 * If plug type is corrected from special headset to headphone,
1243 * clear the micbias enable flag, set micbias back to 1.8V and
1246 if (plug_type == MBHC_PLUG_TYPE_HEADPHONE) {
1247 wcd_micbias_disable(mbhc);
1249 * Enable ADC COMPLETE interrupt for HEADPHONE.
1250 * Btn release may happen after the correct work, ADC COMPLETE
1251 * interrupt needs to be captured to correct plug type.
1253 enable_irq(mbhc->intr_ids->mbhc_hs_ins_intr);
1256 if (mbhc->mbhc_cb->hph_pull_down_ctrl)
1257 mbhc->mbhc_cb->hph_pull_down_ctrl(component, true);
1259 pm_runtime_mark_last_busy(component->dev);
1260 pm_runtime_put_autosuspend(component->dev);
1263 static irqreturn_t wcd_mbhc_adc_hs_rem_irq(int irq, void *data)
1265 struct wcd_mbhc *mbhc = data;
1266 unsigned long timeout;
1267 int adc_threshold, output_mv, retry = 0;
1269 mutex_lock(&mbhc->lock);
1270 timeout = jiffies + msecs_to_jiffies(WCD_FAKE_REMOVAL_MIN_PERIOD_MS);
1271 adc_threshold = wcd_mbhc_adc_get_hs_thres(mbhc);
1276 * read output_mv every 10ms to look for
1277 * any change in IN2_P
1279 usleep_range(10000, 10100);
1280 output_mv = wcd_measure_adc_once(mbhc, MUX_CTL_IN2P);
1282 /* Check for fake removal */
1283 if ((output_mv <= adc_threshold) && retry > FAKE_REM_RETRY_ATTEMPTS)
1285 } while (!time_after(jiffies, timeout));
1288 * ADC COMPLETE and ELEC_REM interrupts are both enabled for
1289 * HEADPHONE, need to reject the ADC COMPLETE interrupt which
1290 * follows ELEC_REM one when HEADPHONE is removed.
1292 if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADPHONE)
1293 mbhc->extn_cable_hph_rem = true;
1295 wcd_mbhc_write_field(mbhc, WCD_MBHC_DETECTION_DONE, 0);
1296 wcd_mbhc_write_field(mbhc, WCD_MBHC_ADC_MODE, 0);
1297 wcd_mbhc_write_field(mbhc, WCD_MBHC_ADC_EN, 0);
1298 wcd_mbhc_elec_hs_report_unplug(mbhc);
1299 wcd_mbhc_write_field(mbhc, WCD_MBHC_BTN_ISRC_CTL, 0);
1302 mutex_unlock(&mbhc->lock);
1306 static irqreturn_t wcd_mbhc_adc_hs_ins_irq(int irq, void *data)
1308 struct wcd_mbhc *mbhc = data;
1310 u8 clamp_retry = WCD_MBHC_FAKE_INS_RETRY;
1313 * ADC COMPLETE and ELEC_REM interrupts are both enabled for HEADPHONE,
1314 * need to reject the ADC COMPLETE interrupt which follows ELEC_REM one
1315 * when HEADPHONE is removed.
1317 if (mbhc->extn_cable_hph_rem == true) {
1318 mbhc->extn_cable_hph_rem = false;
1323 clamp_state = wcd_mbhc_read_field(mbhc, WCD_MBHC_IN2P_CLAMP_STATE);
1327 * check clamp for 120ms but at 30ms chunks to leave
1328 * room for other interrupts to be processed
1330 usleep_range(30000, 30100);
1331 } while (--clamp_retry);
1334 * If current plug is headphone then there is no chance to
1335 * get ADC complete interrupt, so connected cable should be
1336 * headset not headphone.
1338 if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADPHONE) {
1339 disable_irq_nosync(mbhc->intr_ids->mbhc_hs_ins_intr);
1340 wcd_mbhc_write_field(mbhc, WCD_MBHC_DETECTION_DONE, 1);
1341 wcd_mbhc_find_plug_and_report(mbhc, MBHC_PLUG_TYPE_HEADSET);
1348 int wcd_mbhc_get_impedance(struct wcd_mbhc *mbhc, uint32_t *zl, uint32_t *zr)
1358 EXPORT_SYMBOL(wcd_mbhc_get_impedance);
1360 void wcd_mbhc_set_hph_type(struct wcd_mbhc *mbhc, int hph_type)
1362 mbhc->hph_type = hph_type;
1364 EXPORT_SYMBOL(wcd_mbhc_set_hph_type);
1366 int wcd_mbhc_get_hph_type(struct wcd_mbhc *mbhc)
1368 return mbhc->hph_type;
1370 EXPORT_SYMBOL(wcd_mbhc_get_hph_type);
1372 int wcd_mbhc_start(struct wcd_mbhc *mbhc, struct wcd_mbhc_config *cfg,
1373 struct snd_soc_jack *jack)
1375 if (!mbhc || !cfg || !jack)
1381 return wcd_mbhc_initialise(mbhc);
1383 EXPORT_SYMBOL(wcd_mbhc_start);
1385 void wcd_mbhc_stop(struct wcd_mbhc *mbhc)
1387 mbhc->current_plug = MBHC_PLUG_TYPE_NONE;
1388 mbhc->hph_status = 0;
1389 disable_irq_nosync(mbhc->intr_ids->hph_left_ocp);
1390 disable_irq_nosync(mbhc->intr_ids->hph_right_ocp);
1392 EXPORT_SYMBOL(wcd_mbhc_stop);
1394 int wcd_dt_parse_mbhc_data(struct device *dev, struct wcd_mbhc_config *cfg)
1396 struct device_node *np = dev->of_node;
1397 int ret, i, microvolt;
1399 if (of_property_read_bool(np, "qcom,hphl-jack-type-normally-closed"))
1400 cfg->hphl_swh = false;
1402 cfg->hphl_swh = true;
1404 if (of_property_read_bool(np, "qcom,ground-jack-type-normally-closed"))
1405 cfg->gnd_swh = false;
1407 cfg->gnd_swh = true;
1409 ret = of_property_read_u32(np, "qcom,mbhc-headset-vthreshold-microvolt",
1412 dev_dbg(dev, "missing qcom,mbhc-hs-mic-max-vthreshold--microvolt in dt node\n");
1414 cfg->hs_thr = microvolt/1000;
1416 ret = of_property_read_u32(np, "qcom,mbhc-headphone-vthreshold-microvolt",
1419 dev_dbg(dev, "missing qcom,mbhc-hs-mic-min-vthreshold-microvolt entry\n");
1421 cfg->hph_thr = microvolt/1000;
1423 ret = of_property_read_u32_array(np,
1424 "qcom,mbhc-buttons-vthreshold-microvolt",
1426 WCD_MBHC_DEF_BUTTONS);
1428 dev_err(dev, "missing qcom,mbhc-buttons-vthreshold-microvolt entry\n");
1430 for (i = 0; i < WCD_MBHC_DEF_BUTTONS; i++) {
1431 if (ret) /* default voltage */
1432 cfg->btn_high[i] = 500000;
1434 /* Micro to Milli Volts */
1435 cfg->btn_high[i] = cfg->btn_high[i]/1000;
1440 EXPORT_SYMBOL(wcd_dt_parse_mbhc_data);
1442 struct wcd_mbhc *wcd_mbhc_init(struct snd_soc_component *component,
1443 const struct wcd_mbhc_cb *mbhc_cb,
1444 const struct wcd_mbhc_intr *intr_ids,
1445 struct wcd_mbhc_field *fields,
1446 bool impedance_det_en)
1448 struct device *dev = component->dev;
1449 struct wcd_mbhc *mbhc;
1452 if (!intr_ids || !fields || !mbhc_cb || !mbhc_cb->mbhc_bias || !mbhc_cb->set_btn_thr) {
1453 dev_err(dev, "%s: Insufficient mbhc configuration\n", __func__);
1454 return ERR_PTR(-EINVAL);
1457 mbhc = kzalloc(sizeof(*mbhc), GFP_KERNEL);
1459 return ERR_PTR(-ENOMEM);
1461 mbhc->component = component;
1463 mbhc->intr_ids = intr_ids;
1464 mbhc->mbhc_cb = mbhc_cb;
1465 mbhc->fields = fields;
1466 mbhc->mbhc_detection_logic = WCD_DETECTION_ADC;
1468 if (mbhc_cb->compute_impedance)
1469 mbhc->impedance_detect = impedance_det_en;
1471 INIT_DELAYED_WORK(&mbhc->mbhc_btn_dwork, wcd_btn_long_press_fn);
1473 mutex_init(&mbhc->lock);
1475 INIT_WORK(&mbhc->correct_plug_swch, wcd_correct_swch_plug);
1477 ret = request_threaded_irq(mbhc->intr_ids->mbhc_sw_intr, NULL,
1478 wcd_mbhc_mech_plug_detect_irq,
1479 IRQF_ONESHOT | IRQF_TRIGGER_RISING,
1480 "mbhc sw intr", mbhc);
1484 ret = request_threaded_irq(mbhc->intr_ids->mbhc_btn_press_intr, NULL,
1485 wcd_mbhc_btn_press_handler,
1486 IRQF_ONESHOT | IRQF_TRIGGER_RISING,
1487 "Button Press detect", mbhc);
1489 goto err_free_sw_intr;
1491 ret = request_threaded_irq(mbhc->intr_ids->mbhc_btn_release_intr, NULL,
1492 wcd_mbhc_btn_release_handler,
1493 IRQF_ONESHOT | IRQF_TRIGGER_RISING,
1494 "Button Release detect", mbhc);
1496 goto err_free_btn_press_intr;
1498 ret = request_threaded_irq(mbhc->intr_ids->mbhc_hs_ins_intr, NULL,
1499 wcd_mbhc_adc_hs_ins_irq,
1500 IRQF_ONESHOT | IRQF_TRIGGER_RISING,
1501 "Elect Insert", mbhc);
1503 goto err_free_btn_release_intr;
1505 disable_irq_nosync(mbhc->intr_ids->mbhc_hs_ins_intr);
1507 ret = request_threaded_irq(mbhc->intr_ids->mbhc_hs_rem_intr, NULL,
1508 wcd_mbhc_adc_hs_rem_irq,
1509 IRQF_ONESHOT | IRQF_TRIGGER_RISING,
1510 "Elect Remove", mbhc);
1512 goto err_free_hs_ins_intr;
1514 disable_irq_nosync(mbhc->intr_ids->mbhc_hs_rem_intr);
1516 ret = request_threaded_irq(mbhc->intr_ids->hph_left_ocp, NULL,
1517 wcd_mbhc_hphl_ocp_irq,
1518 IRQF_ONESHOT | IRQF_TRIGGER_RISING,
1519 "HPH_L OCP detect", mbhc);
1521 goto err_free_hs_rem_intr;
1523 ret = request_threaded_irq(mbhc->intr_ids->hph_right_ocp, NULL,
1524 wcd_mbhc_hphr_ocp_irq,
1525 IRQF_ONESHOT | IRQF_TRIGGER_RISING,
1526 "HPH_R OCP detect", mbhc);
1528 goto err_free_hph_left_ocp;
1532 err_free_hph_left_ocp:
1533 free_irq(mbhc->intr_ids->hph_left_ocp, mbhc);
1534 err_free_hs_rem_intr:
1535 free_irq(mbhc->intr_ids->mbhc_hs_rem_intr, mbhc);
1536 err_free_hs_ins_intr:
1537 free_irq(mbhc->intr_ids->mbhc_hs_ins_intr, mbhc);
1538 err_free_btn_release_intr:
1539 free_irq(mbhc->intr_ids->mbhc_btn_release_intr, mbhc);
1540 err_free_btn_press_intr:
1541 free_irq(mbhc->intr_ids->mbhc_btn_press_intr, mbhc);
1543 free_irq(mbhc->intr_ids->mbhc_sw_intr, mbhc);
1547 dev_err(dev, "Failed to request mbhc interrupts %d\n", ret);
1549 return ERR_PTR(ret);
1551 EXPORT_SYMBOL(wcd_mbhc_init);
1553 void wcd_mbhc_deinit(struct wcd_mbhc *mbhc)
1555 free_irq(mbhc->intr_ids->hph_right_ocp, mbhc);
1556 free_irq(mbhc->intr_ids->hph_left_ocp, mbhc);
1557 free_irq(mbhc->intr_ids->mbhc_hs_rem_intr, mbhc);
1558 free_irq(mbhc->intr_ids->mbhc_hs_ins_intr, mbhc);
1559 free_irq(mbhc->intr_ids->mbhc_btn_release_intr, mbhc);
1560 free_irq(mbhc->intr_ids->mbhc_btn_press_intr, mbhc);
1561 free_irq(mbhc->intr_ids->mbhc_sw_intr, mbhc);
1563 mutex_lock(&mbhc->lock);
1564 wcd_cancel_hs_detect_plug(mbhc, &mbhc->correct_plug_swch);
1565 mutex_unlock(&mbhc->lock);
1569 EXPORT_SYMBOL(wcd_mbhc_deinit);
1571 static int __init mbhc_init(void)
1576 static void __exit mbhc_exit(void)
1580 module_init(mbhc_init);
1581 module_exit(mbhc_exit);
1583 MODULE_DESCRIPTION("wcd MBHC v2 module");
1584 MODULE_LICENSE("GPL");