wcd-mbhc-adc.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  3. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include <linux/slab.h>
  8. #include <linux/of_gpio.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/device.h>
  11. #include <linux/printk.h>
  12. #include <linux/ratelimit.h>
  13. #include <linux/list.h>
  14. #include <linux/bitops.h>
  15. #include <linux/delay.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/kernel.h>
  18. #include <linux/input.h>
  19. #include <linux/firmware.h>
  20. #include <linux/completion.h>
  21. #include <sound/soc.h>
  22. #include <sound/jack.h>
  23. #include "wcd-mbhc-adc.h"
  24. #include <asoc/wcd-mbhc-v2.h>
  25. #include <asoc/pdata.h>
  26. #define WCD_MBHC_ADC_HS_THRESHOLD_MV 1700
  27. #define WCD_MBHC_ADC_HPH_THRESHOLD_MV 75
  28. #define WCD_MBHC_ADC_MICBIAS_MV 1800
  29. #define WCD_MBHC_FAKE_INS_RETRY 4
  30. static int wcd_mbhc_get_micbias(struct wcd_mbhc *mbhc)
  31. {
  32. int micbias = 0;
  33. u8 vout_ctl = 0;
  34. if (mbhc->mbhc_cb->get_micbias_val) {
  35. mbhc->mbhc_cb->get_micbias_val(mbhc, &micbias);
  36. pr_debug("%s: micbias: %d\n", __func__, micbias);
  37. } else {
  38. /* Read MBHC Micbias (Mic Bias2) voltage */
  39. WCD_MBHC_REG_READ(WCD_MBHC_MICB2_VOUT, vout_ctl);
  40. /* Formula for getting micbias from vout
  41. * micbias = 1.0V + VOUT_CTL * 50mV
  42. */
  43. micbias = 1000 + (vout_ctl * 50);
  44. pr_debug("%s: vout_ctl: %d, micbias: %d\n",
  45. __func__, vout_ctl, micbias);
  46. }
  47. return micbias;
  48. }
  49. static int wcd_get_voltage_from_adc(u8 val, int micbias)
  50. {
  51. /* Formula for calculating voltage from ADC
  52. * Voltage = ADC_RESULT*12.5mV*V_MICBIAS/1.8
  53. */
  54. return ((val * 125 * micbias)/(WCD_MBHC_ADC_MICBIAS_MV * 10));
  55. }
  56. static int wcd_measure_adc_continuous(struct wcd_mbhc *mbhc)
  57. {
  58. u8 adc_result = 0;
  59. int output_mv = 0;
  60. int retry = 3;
  61. u8 adc_en = 0;
  62. pr_debug("%s: enter\n", __func__);
  63. /* Pre-requisites for ADC continuous measurement */
  64. /* Read legacy electircal detection and disable */
  65. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_SCHMT_ISRC, 0x00);
  66. /* Set ADC to continuous measurement */
  67. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_MODE, 1);
  68. /* Read ADC Enable bit to restore after adc measurement */
  69. WCD_MBHC_REG_READ(WCD_MBHC_ADC_EN, adc_en);
  70. /* Disable ADC_ENABLE bit */
  71. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_EN, 0);
  72. /* Disable MBHC FSM */
  73. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  74. /* Set the MUX selection to IN2P */
  75. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MUX_CTL, MUX_CTL_IN2P);
  76. /*
  77. * Current source mode requires Auto zeroing to be enabled
  78. * automatically. If HW doesn't do it, SW has to take care of this
  79. * for button interrupts to work fine and to avoid
  80. * fake electrical removal interrupts by enabling autozero before FSM
  81. * enable and disable it after FSM enable
  82. */
  83. if (mbhc->mbhc_cb->mbhc_comp_autozero_control)
  84. mbhc->mbhc_cb->mbhc_comp_autozero_control(mbhc,
  85. true);
  86. /* Enable MBHC FSM */
  87. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 1);
  88. if (mbhc->mbhc_cb->mbhc_comp_autozero_control)
  89. mbhc->mbhc_cb->mbhc_comp_autozero_control(mbhc,
  90. false);
  91. /* Enable ADC_ENABLE bit */
  92. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_EN, 1);
  93. while (retry--) {
  94. /* wait for 3 msec before reading ADC result */
  95. usleep_range(3000, 3100);
  96. /* Read ADC result */
  97. WCD_MBHC_REG_READ(WCD_MBHC_ADC_RESULT, adc_result);
  98. }
  99. /* Restore ADC Enable */
  100. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_EN, adc_en);
  101. /* Get voltage from ADC result */
  102. output_mv = wcd_get_voltage_from_adc(adc_result,
  103. wcd_mbhc_get_micbias(mbhc));
  104. pr_debug("%s: adc_result: 0x%x, output_mv: %d\n",
  105. __func__, adc_result, output_mv);
  106. return output_mv;
  107. }
  108. static int wcd_measure_adc_once(struct wcd_mbhc *mbhc, int mux_ctl)
  109. {
  110. u8 adc_timeout = 0;
  111. u8 adc_complete = 0;
  112. u8 adc_result = 0;
  113. int retry = 6;
  114. int ret = 0;
  115. int output_mv = 0;
  116. u8 adc_en = 0;
  117. pr_debug("%s: enter\n", __func__);
  118. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_MODE, 0);
  119. /* Read ADC Enable bit to restore after adc measurement */
  120. WCD_MBHC_REG_READ(WCD_MBHC_ADC_EN, adc_en);
  121. /* Trigger ADC one time measurement */
  122. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_EN, 0);
  123. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  124. /* Set the appropriate MUX selection */
  125. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MUX_CTL, mux_ctl);
  126. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 1);
  127. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_EN, 1);
  128. while (retry--) {
  129. /* wait for 600usec to get adc results */
  130. usleep_range(600, 610);
  131. /* check for ADC Timeout */
  132. WCD_MBHC_REG_READ(WCD_MBHC_ADC_TIMEOUT, adc_timeout);
  133. if (adc_timeout)
  134. continue;
  135. /* Read ADC complete bit */
  136. WCD_MBHC_REG_READ(WCD_MBHC_ADC_COMPLETE, adc_complete);
  137. if (!adc_complete)
  138. continue;
  139. /* Read ADC result */
  140. WCD_MBHC_REG_READ(WCD_MBHC_ADC_RESULT, adc_result);
  141. pr_debug("%s: ADC result: 0x%x\n", __func__, adc_result);
  142. /* Get voltage from ADC result */
  143. output_mv = wcd_get_voltage_from_adc(adc_result,
  144. wcd_mbhc_get_micbias(mbhc));
  145. break;
  146. }
  147. /* Restore ADC Enable */
  148. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_EN, adc_en);
  149. if (retry <= 0) {
  150. pr_err("%s: adc complete: %d, adc timeout: %d\n",
  151. __func__, adc_complete, adc_timeout);
  152. ret = -EINVAL;
  153. } else {
  154. pr_debug("%s: adc complete: %d, adc timeout: %d output_mV: %d\n",
  155. __func__, adc_complete, adc_timeout, output_mv);
  156. ret = output_mv;
  157. }
  158. pr_debug("%s: leave\n", __func__);
  159. return ret;
  160. }
  161. static bool wcd_mbhc_adc_detect_anc_plug_type(struct wcd_mbhc *mbhc)
  162. {
  163. bool anc_mic_found = false;
  164. u16 fsm_en = 0;
  165. u8 det = 0;
  166. unsigned long retry = 0;
  167. int valid_plug_cnt = 0, invalid_plug_cnt = 0;
  168. int ret = 0;
  169. u8 elect_ctl = 0;
  170. u8 adc_mode = 0;
  171. u8 vref = 0;
  172. int vref_mv[] = {1650, 1500, 1600, 1700};
  173. if (mbhc->mbhc_cfg->anc_micbias < MIC_BIAS_1 ||
  174. mbhc->mbhc_cfg->anc_micbias > MIC_BIAS_4)
  175. return false;
  176. if (!mbhc->mbhc_cb->mbhc_micbias_control)
  177. return false;
  178. /* Disable Detection done for ADC operation */
  179. WCD_MBHC_REG_READ(WCD_MBHC_DETECTION_DONE, det);
  180. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_DETECTION_DONE, 0);
  181. /* Mask ADC COMPLETE interrupt */
  182. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS, false);
  183. WCD_MBHC_REG_READ(WCD_MBHC_FSM_EN, fsm_en);
  184. mbhc->mbhc_cb->mbhc_micbias_control(mbhc->component,
  185. mbhc->mbhc_cfg->anc_micbias,
  186. MICB_ENABLE);
  187. /* Read legacy electircal detection and disable */
  188. WCD_MBHC_REG_READ(WCD_MBHC_ELECT_SCHMT_ISRC, elect_ctl);
  189. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_SCHMT_ISRC, 0x00);
  190. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ANC_DET_EN, 1);
  191. WCD_MBHC_REG_READ(WCD_MBHC_ADC_MODE, adc_mode);
  192. /*
  193. * wait for button debounce time 20ms. If 4-pole plug is inserted
  194. * into 5-pole jack, then there will be a button press interrupt
  195. * during anc plug detection. In that case though Hs_comp_res is 0,
  196. * it should not be declared as ANC plug type
  197. */
  198. usleep_range(20000, 20100);
  199. /*
  200. * After enabling FSM, to handle slow insertion scenarios,
  201. * check IN3 voltage is below the Vref
  202. */
  203. WCD_MBHC_REG_READ(WCD_MBHC_HS_VREF, vref);
  204. do {
  205. if (wcd_swch_level_remove(mbhc)) {
  206. pr_debug("%s: Switch level is low\n", __func__);
  207. goto done;
  208. }
  209. pr_debug("%s: Retry attempt %lu\n", __func__, retry + 1);
  210. ret = wcd_measure_adc_once(mbhc, MUX_CTL_IN3P);
  211. /* TODO - check the logic */
  212. if (ret && (ret < vref_mv[vref]))
  213. valid_plug_cnt++;
  214. else
  215. invalid_plug_cnt++;
  216. retry++;
  217. } while (retry < ANC_DETECT_RETRY_CNT);
  218. pr_debug("%s: valid: %d, invalid: %d\n", __func__, valid_plug_cnt,
  219. invalid_plug_cnt);
  220. /* decision logic */
  221. if (valid_plug_cnt > invalid_plug_cnt)
  222. anc_mic_found = true;
  223. done:
  224. /* Restore ADC mode */
  225. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_MODE, adc_mode);
  226. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ANC_DET_EN, 0);
  227. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  228. /* Set the MUX selection to AUTO */
  229. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MUX_CTL, MUX_CTL_AUTO);
  230. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 1);
  231. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, fsm_en);
  232. /* Restore detection done */
  233. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_DETECTION_DONE, det);
  234. /* Restore electrical detection */
  235. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_SCHMT_ISRC, elect_ctl);
  236. mbhc->mbhc_cb->mbhc_micbias_control(mbhc->component,
  237. mbhc->mbhc_cfg->anc_micbias,
  238. MICB_DISABLE);
  239. pr_debug("%s: anc mic %sfound\n", __func__,
  240. anc_mic_found ? "" : "not ");
  241. return anc_mic_found;
  242. }
  243. /* To determine if cross connection occurred */
  244. static int wcd_check_cross_conn(struct wcd_mbhc *mbhc)
  245. {
  246. enum wcd_mbhc_plug_type plug_type = MBHC_PLUG_TYPE_NONE;
  247. int hphl_adc_res = 0, hphr_adc_res = 0;
  248. u8 fsm_en = 0;
  249. int ret = 0;
  250. u8 adc_mode = 0;
  251. u8 elect_ctl = 0;
  252. u8 adc_en = 0;
  253. pr_debug("%s: enter\n", __func__);
  254. /* Check for button press and plug detection */
  255. if (wcd_swch_level_remove(mbhc)) {
  256. pr_debug("%s: Switch level is low\n", __func__);
  257. return -EINVAL;
  258. }
  259. /* If PA is enabled, dont check for cross-connection */
  260. if (mbhc->mbhc_cb->hph_pa_on_status)
  261. if (mbhc->mbhc_cb->hph_pa_on_status(mbhc->component))
  262. return -EINVAL;
  263. /* Read legacy electircal detection and disable */
  264. WCD_MBHC_REG_READ(WCD_MBHC_ELECT_SCHMT_ISRC, elect_ctl);
  265. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_SCHMT_ISRC, 0x00);
  266. /* Disable surge detection before ADC measurement */
  267. if (mbhc->mbhc_cb->mbhc_surge_ctl)
  268. mbhc->mbhc_cb->mbhc_surge_ctl(mbhc, false);
  269. /* Read and set ADC to single measurement */
  270. WCD_MBHC_REG_READ(WCD_MBHC_ADC_MODE, adc_mode);
  271. /* Read ADC Enable bit to restore after adc measurement */
  272. WCD_MBHC_REG_READ(WCD_MBHC_ADC_EN, adc_en);
  273. /* Read FSM status */
  274. WCD_MBHC_REG_READ(WCD_MBHC_FSM_EN, fsm_en);
  275. /* Get adc result for HPH L */
  276. hphl_adc_res = wcd_measure_adc_once(mbhc, MUX_CTL_HPH_L);
  277. if (hphl_adc_res < 0) {
  278. pr_err("%s: hphl_adc_res adc measurement failed\n", __func__);
  279. ret = hphl_adc_res;
  280. goto done;
  281. }
  282. /* Get adc result for HPH R in mV */
  283. hphr_adc_res = wcd_measure_adc_once(mbhc, MUX_CTL_HPH_R);
  284. if (hphr_adc_res < 0) {
  285. pr_err("%s: hphr_adc_res adc measurement failed\n", __func__);
  286. ret = hphr_adc_res;
  287. goto done;
  288. }
  289. /* Update cross connection threshold voltages if needed */
  290. if (mbhc->mbhc_cb->update_cross_conn_thr)
  291. mbhc->mbhc_cb->update_cross_conn_thr(mbhc);
  292. if (hphl_adc_res > mbhc->hphl_cross_conn_thr &&
  293. hphr_adc_res > mbhc->hphr_cross_conn_thr) {
  294. plug_type = MBHC_PLUG_TYPE_GND_MIC_SWAP;
  295. pr_debug("%s: Cross connection identified\n", __func__);
  296. } else {
  297. pr_debug("%s: No Cross connection found\n", __func__);
  298. }
  299. done:
  300. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  301. /* Set the MUX selection to Auto */
  302. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MUX_CTL, MUX_CTL_AUTO);
  303. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 1);
  304. /* Restore ADC Enable */
  305. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_EN, adc_en);
  306. /* Restore ADC mode */
  307. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_MODE, adc_mode);
  308. /* Restore FSM state */
  309. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, fsm_en);
  310. /* Restore surge detection */
  311. if (mbhc->mbhc_cb->mbhc_surge_ctl)
  312. mbhc->mbhc_cb->mbhc_surge_ctl(mbhc, true);
  313. /* Restore electrical detection */
  314. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_SCHMT_ISRC, elect_ctl);
  315. pr_debug("%s: leave, plug type: %d\n", __func__, plug_type);
  316. return (plug_type == MBHC_PLUG_TYPE_GND_MIC_SWAP) ? true : false;
  317. }
  318. static int wcd_mbhc_adc_get_spl_hs_thres(struct wcd_mbhc *mbhc)
  319. {
  320. int hs_threshold, micbias_mv;
  321. micbias_mv = wcd_mbhc_get_micbias(mbhc);
  322. if (mbhc->hs_thr && mbhc->micb_mv != WCD_MBHC_ADC_MICBIAS_MV) {
  323. if (mbhc->micb_mv == micbias_mv)
  324. hs_threshold = mbhc->hs_thr;
  325. else
  326. hs_threshold = (mbhc->hs_thr *
  327. micbias_mv) / mbhc->micb_mv;
  328. } else {
  329. hs_threshold = ((WCD_MBHC_ADC_HS_THRESHOLD_MV *
  330. micbias_mv) / WCD_MBHC_ADC_MICBIAS_MV);
  331. }
  332. return hs_threshold;
  333. }
  334. static int wcd_mbhc_adc_get_hs_thres(struct wcd_mbhc *mbhc)
  335. {
  336. int hs_threshold, micbias_mv;
  337. micbias_mv = wcd_mbhc_get_micbias(mbhc);
  338. if (mbhc->hs_thr) {
  339. if (mbhc->micb_mv == micbias_mv)
  340. hs_threshold = mbhc->hs_thr;
  341. else
  342. hs_threshold = (mbhc->hs_thr *
  343. micbias_mv) / mbhc->micb_mv;
  344. } else {
  345. hs_threshold = ((WCD_MBHC_ADC_HS_THRESHOLD_MV *
  346. micbias_mv) / WCD_MBHC_ADC_MICBIAS_MV);
  347. }
  348. return hs_threshold;
  349. }
  350. static int wcd_mbhc_adc_get_hph_thres(struct wcd_mbhc *mbhc)
  351. {
  352. int hph_threshold, micbias_mv;
  353. micbias_mv = wcd_mbhc_get_micbias(mbhc);
  354. if (mbhc->hph_thr) {
  355. if (mbhc->micb_mv == micbias_mv)
  356. hph_threshold = mbhc->hph_thr;
  357. else
  358. hph_threshold = (mbhc->hph_thr *
  359. micbias_mv) / mbhc->micb_mv;
  360. } else {
  361. hph_threshold = ((WCD_MBHC_ADC_HPH_THRESHOLD_MV *
  362. micbias_mv) / WCD_MBHC_ADC_MICBIAS_MV);
  363. }
  364. return hph_threshold;
  365. }
  366. static bool wcd_mbhc_adc_check_for_spl_headset(struct wcd_mbhc *mbhc,
  367. int *spl_hs_cnt)
  368. {
  369. bool spl_hs = false;
  370. int output_mv = 0;
  371. int adc_threshold = 0, adc_hph_threshold = 0;
  372. pr_debug("%s: enter\n", __func__);
  373. if (!mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic)
  374. goto exit;
  375. /* Bump up MB2 to 2.7V */
  376. mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic(mbhc->component,
  377. mbhc->mbhc_cfg->mbhc_micbias, true);
  378. usleep_range(10000, 10100);
  379. /*
  380. * Use ADC single mode to minimize the chance of missing out
  381. * btn press/relesae for HEADSET type during correct work.
  382. */
  383. output_mv = wcd_measure_adc_once(mbhc, MUX_CTL_IN2P);
  384. adc_threshold = wcd_mbhc_adc_get_spl_hs_thres(mbhc);
  385. adc_hph_threshold = wcd_mbhc_adc_get_hph_thres(mbhc);
  386. if (output_mv > adc_threshold || output_mv < adc_hph_threshold) {
  387. spl_hs = false;
  388. } else {
  389. spl_hs = true;
  390. if (spl_hs_cnt)
  391. *spl_hs_cnt += 1;
  392. }
  393. /* MB2 back to 1.8v if the type is not special headset */
  394. if (spl_hs_cnt && (*spl_hs_cnt != WCD_MBHC_SPL_HS_CNT)) {
  395. mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic(mbhc->component,
  396. mbhc->mbhc_cfg->mbhc_micbias, false);
  397. /* Add 10ms delay for micbias to settle */
  398. usleep_range(10000, 10100);
  399. }
  400. exit:
  401. pr_debug("%s: leave\n", __func__);
  402. return spl_hs;
  403. }
  404. static bool wcd_is_special_headset(struct wcd_mbhc *mbhc)
  405. {
  406. int delay = 0;
  407. bool ret = false;
  408. bool is_spl_hs = false;
  409. int output_mv = 0;
  410. int adc_threshold = 0;
  411. /*
  412. * Increase micbias to 2.7V to detect headsets with
  413. * threshold on microphone
  414. */
  415. if (mbhc->mbhc_cb->mbhc_micbias_control &&
  416. !mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic) {
  417. pr_debug("%s: callback fn micb_ctrl_thr_mic not defined\n",
  418. __func__);
  419. return false;
  420. } else if (mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic) {
  421. ret = mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic(mbhc->component,
  422. MIC_BIAS_2, true);
  423. if (ret) {
  424. pr_err("%s: mbhc_micb_ctrl_thr_mic failed, ret: %d\n",
  425. __func__, ret);
  426. return false;
  427. }
  428. }
  429. adc_threshold = wcd_mbhc_adc_get_spl_hs_thres(mbhc);
  430. while (!is_spl_hs) {
  431. if (mbhc->hs_detect_work_stop) {
  432. pr_debug("%s: stop requested: %d\n", __func__,
  433. mbhc->hs_detect_work_stop);
  434. break;
  435. }
  436. delay += 50;
  437. /* Wait for 50ms for FSM to update result */
  438. msleep(50);
  439. output_mv = wcd_measure_adc_once(mbhc, MUX_CTL_IN2P);
  440. if (output_mv <= adc_threshold) {
  441. pr_debug("%s: Special headset detected in %d msecs\n",
  442. __func__, delay);
  443. is_spl_hs = true;
  444. }
  445. if (delay == SPECIAL_HS_DETECT_TIME_MS) {
  446. pr_debug("%s: Spl headset not found in 2 sec\n",
  447. __func__);
  448. break;
  449. }
  450. }
  451. if (is_spl_hs) {
  452. pr_debug("%s: Headset with threshold found\n", __func__);
  453. mbhc->micbias_enable = true;
  454. ret = true;
  455. }
  456. if (mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic &&
  457. !mbhc->micbias_enable)
  458. mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic(mbhc->component,
  459. MIC_BIAS_2,
  460. false);
  461. pr_debug("%s: leave, micb_enable: %d\n", __func__,
  462. mbhc->micbias_enable);
  463. return ret;
  464. }
  465. static void wcd_mbhc_adc_update_fsm_source(struct wcd_mbhc *mbhc,
  466. enum wcd_mbhc_plug_type plug_type)
  467. {
  468. bool micbias2;
  469. micbias2 = mbhc->mbhc_cb->micbias_enable_status(mbhc,
  470. MIC_BIAS_2);
  471. switch (plug_type) {
  472. case MBHC_PLUG_TYPE_HEADPHONE:
  473. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 3);
  474. break;
  475. case MBHC_PLUG_TYPE_HEADSET:
  476. case MBHC_PLUG_TYPE_ANC_HEADPHONE:
  477. if (!mbhc->is_hs_recording && !micbias2)
  478. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 3);
  479. break;
  480. default:
  481. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  482. break;
  483. };
  484. }
  485. /* should be called under interrupt context that hold suspend */
  486. static void wcd_schedule_hs_detect_plug(struct wcd_mbhc *mbhc,
  487. struct work_struct *work)
  488. {
  489. pr_debug("%s: scheduling correct_swch_plug\n", __func__);
  490. WCD_MBHC_RSC_ASSERT_LOCKED(mbhc);
  491. mbhc->hs_detect_work_stop = false;
  492. mbhc->mbhc_cb->lock_sleep(mbhc, true);
  493. schedule_work(work);
  494. }
  495. /* called under codec_resource_lock acquisition */
  496. static void wcd_cancel_hs_detect_plug(struct wcd_mbhc *mbhc,
  497. struct work_struct *work)
  498. {
  499. pr_debug("%s: Canceling correct_plug_swch\n", __func__);
  500. mbhc->hs_detect_work_stop = true;
  501. WCD_MBHC_RSC_UNLOCK(mbhc);
  502. if (cancel_work_sync(work)) {
  503. pr_debug("%s: correct_plug_swch is canceled\n",
  504. __func__);
  505. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  506. }
  507. WCD_MBHC_RSC_LOCK(mbhc);
  508. }
  509. /* called under codec_resource_lock acquisition */
  510. static void wcd_mbhc_adc_detect_plug_type(struct wcd_mbhc *mbhc)
  511. {
  512. struct snd_soc_component *component = mbhc->component;
  513. pr_debug("%s: enter\n", __func__);
  514. WCD_MBHC_RSC_ASSERT_LOCKED(mbhc);
  515. if (mbhc->mbhc_cb->hph_pull_down_ctrl)
  516. mbhc->mbhc_cb->hph_pull_down_ctrl(component, false);
  517. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_DETECTION_DONE, 0);
  518. if (mbhc->mbhc_cb->mbhc_micbias_control) {
  519. mbhc->mbhc_cb->mbhc_micbias_control(component, MIC_BIAS_2,
  520. MICB_ENABLE);
  521. } else {
  522. pr_err("%s: Mic Bias is not enabled\n", __func__);
  523. return;
  524. }
  525. /* Re-initialize button press completion object */
  526. reinit_completion(&mbhc->btn_press_compl);
  527. wcd_schedule_hs_detect_plug(mbhc, &mbhc->correct_plug_swch);
  528. pr_debug("%s: leave\n", __func__);
  529. }
  530. static void wcd_micbias_disable(struct wcd_mbhc *mbhc)
  531. {
  532. if (mbhc->micbias_enable) {
  533. mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic(
  534. mbhc->component, MIC_BIAS_2, false);
  535. if (mbhc->mbhc_cb->set_micbias_value)
  536. mbhc->mbhc_cb->set_micbias_value(
  537. mbhc->component);
  538. mbhc->micbias_enable = false;
  539. }
  540. }
  541. static int wcd_mbhc_get_plug_from_adc(struct wcd_mbhc *mbhc, int adc_result)
  542. {
  543. enum wcd_mbhc_plug_type plug_type = MBHC_PLUG_TYPE_INVALID;
  544. u32 hph_thr = 0, hs_thr = 0;
  545. hs_thr = wcd_mbhc_adc_get_hs_thres(mbhc);
  546. hph_thr = wcd_mbhc_adc_get_hph_thres(mbhc);
  547. if (adc_result < hph_thr)
  548. plug_type = MBHC_PLUG_TYPE_HEADPHONE;
  549. else if (adc_result > hs_thr)
  550. plug_type = MBHC_PLUG_TYPE_HIGH_HPH;
  551. else
  552. plug_type = MBHC_PLUG_TYPE_HEADSET;
  553. pr_debug("%s: plug type is %d found\n", __func__, plug_type);
  554. return plug_type;
  555. }
  556. static void wcd_correct_swch_plug(struct work_struct *work)
  557. {
  558. struct wcd_mbhc *mbhc;
  559. struct snd_soc_component *component;
  560. enum wcd_mbhc_plug_type plug_type = MBHC_PLUG_TYPE_INVALID;
  561. unsigned long timeout;
  562. bool wrk_complete = false;
  563. int pt_gnd_mic_swap_cnt = 0;
  564. int no_gnd_mic_swap_cnt = 0;
  565. bool is_pa_on = false, spl_hs = false, spl_hs_reported = false;
  566. int ret = 0;
  567. int spl_hs_count = 0;
  568. int output_mv = 0;
  569. int cross_conn;
  570. int try = 0;
  571. int hs_threshold, micbias_mv;
  572. pr_debug("%s: enter\n", __func__);
  573. mbhc = container_of(work, struct wcd_mbhc, correct_plug_swch);
  574. component = mbhc->component;
  575. micbias_mv = wcd_mbhc_get_micbias(mbhc);
  576. hs_threshold = wcd_mbhc_adc_get_hs_thres(mbhc);
  577. WCD_MBHC_RSC_LOCK(mbhc);
  578. /* Mask ADC COMPLETE interrupt */
  579. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS, false);
  580. WCD_MBHC_RSC_UNLOCK(mbhc);
  581. /* Check for cross connection */
  582. do {
  583. cross_conn = wcd_check_cross_conn(mbhc);
  584. try++;
  585. } while (try < mbhc->swap_thr);
  586. if (cross_conn > 0) {
  587. plug_type = MBHC_PLUG_TYPE_GND_MIC_SWAP;
  588. pr_debug("%s: cross connection found, Plug type %d\n",
  589. __func__, plug_type);
  590. goto correct_plug_type;
  591. }
  592. /* Find plug type */
  593. output_mv = wcd_measure_adc_continuous(mbhc);
  594. plug_type = wcd_mbhc_get_plug_from_adc(mbhc, output_mv);
  595. /*
  596. * Report plug type if it is either headset or headphone
  597. * else start the 3 sec loop
  598. */
  599. if ((plug_type == MBHC_PLUG_TYPE_HEADSET ||
  600. plug_type == MBHC_PLUG_TYPE_HEADPHONE) &&
  601. (!wcd_swch_level_remove(mbhc))) {
  602. WCD_MBHC_RSC_LOCK(mbhc);
  603. wcd_mbhc_find_plug_and_report(mbhc, plug_type);
  604. WCD_MBHC_RSC_UNLOCK(mbhc);
  605. }
  606. /*
  607. * Set DETECTION_DONE bit for HEADSET and ANC_HEADPHONE,
  608. * so that btn press/release interrupt can be generated.
  609. */
  610. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADSET ||
  611. mbhc->current_plug == MBHC_PLUG_TYPE_ANC_HEADPHONE) {
  612. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_MODE, 0);
  613. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_EN, 0);
  614. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_DETECTION_DONE, 1);
  615. }
  616. correct_plug_type:
  617. /*
  618. * Callback to disable BCS slow insertion detection
  619. */
  620. if (mbhc->mbhc_cb->bcs_enable)
  621. mbhc->mbhc_cb->bcs_enable(mbhc, false);
  622. timeout = jiffies + msecs_to_jiffies(HS_DETECT_PLUG_TIME_MS);
  623. while (!time_after(jiffies, timeout)) {
  624. if (mbhc->hs_detect_work_stop) {
  625. pr_debug("%s: stop requested: %d\n", __func__,
  626. mbhc->hs_detect_work_stop);
  627. wcd_micbias_disable(mbhc);
  628. goto exit;
  629. }
  630. /* allow sometime and re-check stop requested again */
  631. msleep(20);
  632. if (mbhc->hs_detect_work_stop) {
  633. pr_debug("%s: stop requested: %d\n", __func__,
  634. mbhc->hs_detect_work_stop);
  635. wcd_micbias_disable(mbhc);
  636. goto exit;
  637. }
  638. msleep(180);
  639. /*
  640. * Use ADC single mode to minimize the chance of missing out
  641. * btn press/release for HEADSET type during correct work.
  642. */
  643. output_mv = wcd_measure_adc_once(mbhc, MUX_CTL_IN2P);
  644. /*
  645. * instead of hogging system by contineous polling, wait for
  646. * sometime and re-check stop request again.
  647. */
  648. plug_type = wcd_mbhc_get_plug_from_adc(mbhc, output_mv);
  649. if ((output_mv > hs_threshold) &&
  650. (spl_hs_count < WCD_MBHC_SPL_HS_CNT)) {
  651. spl_hs = wcd_mbhc_adc_check_for_spl_headset(mbhc,
  652. &spl_hs_count);
  653. if (spl_hs)
  654. pr_debug("%s: Detected special HS (%d)\n",
  655. __func__, spl_hs);
  656. output_mv = wcd_measure_adc_once(mbhc, MUX_CTL_IN2P);
  657. if (spl_hs_count == WCD_MBHC_SPL_HS_CNT) {
  658. hs_threshold = (hs_threshold *
  659. wcd_mbhc_get_micbias(mbhc)) / micbias_mv;
  660. spl_hs = true;
  661. mbhc->micbias_enable = true;
  662. }
  663. }
  664. if (mbhc->mbhc_cb->hph_pa_on_status)
  665. is_pa_on = mbhc->mbhc_cb->hph_pa_on_status(
  666. mbhc->component);
  667. if ((output_mv <= hs_threshold) &&
  668. (!is_pa_on)) {
  669. /* Check for cross connection*/
  670. ret = wcd_check_cross_conn(mbhc);
  671. if (ret < 0)
  672. continue;
  673. else if (ret > 0) {
  674. pt_gnd_mic_swap_cnt++;
  675. no_gnd_mic_swap_cnt = 0;
  676. if (pt_gnd_mic_swap_cnt <
  677. mbhc->swap_thr) {
  678. continue;
  679. } else if (pt_gnd_mic_swap_cnt >
  680. mbhc->swap_thr) {
  681. /*
  682. * This is due to GND/MIC switch didn't
  683. * work, Report unsupported plug.
  684. */
  685. pr_debug("%s: switch did not work\n",
  686. __func__);
  687. plug_type = MBHC_PLUG_TYPE_GND_MIC_SWAP;
  688. goto report;
  689. } else {
  690. plug_type = MBHC_PLUG_TYPE_GND_MIC_SWAP;
  691. }
  692. } else {
  693. no_gnd_mic_swap_cnt++;
  694. pt_gnd_mic_swap_cnt = 0;
  695. plug_type = wcd_mbhc_get_plug_from_adc(
  696. mbhc, output_mv);
  697. if ((no_gnd_mic_swap_cnt <
  698. mbhc->swap_thr) &&
  699. (spl_hs_count != WCD_MBHC_SPL_HS_CNT)) {
  700. continue;
  701. } else {
  702. no_gnd_mic_swap_cnt = 0;
  703. }
  704. }
  705. if ((pt_gnd_mic_swap_cnt == mbhc->swap_thr) &&
  706. (plug_type == MBHC_PLUG_TYPE_GND_MIC_SWAP)) {
  707. /*
  708. * if switch is toggled, check again,
  709. * otherwise report unsupported plug
  710. */
  711. if (mbhc->mbhc_cfg->swap_gnd_mic &&
  712. mbhc->mbhc_cfg->swap_gnd_mic(component,
  713. true)) {
  714. pr_debug("%s: US_EU gpio present,flip switch\n"
  715. , __func__);
  716. continue;
  717. }
  718. }
  719. }
  720. if (output_mv > hs_threshold) {
  721. pr_debug("%s: cable is extension cable\n", __func__);
  722. plug_type = MBHC_PLUG_TYPE_HIGH_HPH;
  723. wrk_complete = true;
  724. } else {
  725. pr_debug("%s: cable might be headset: %d\n", __func__,
  726. plug_type);
  727. if (plug_type != MBHC_PLUG_TYPE_GND_MIC_SWAP) {
  728. plug_type = wcd_mbhc_get_plug_from_adc(
  729. mbhc, output_mv);
  730. if (!spl_hs_reported &&
  731. spl_hs_count == WCD_MBHC_SPL_HS_CNT) {
  732. spl_hs_reported = true;
  733. WCD_MBHC_RSC_LOCK(mbhc);
  734. wcd_mbhc_find_plug_and_report(mbhc,
  735. plug_type);
  736. WCD_MBHC_RSC_UNLOCK(mbhc);
  737. continue;
  738. } else if (spl_hs_reported)
  739. continue;
  740. /*
  741. * Report headset only if not already reported
  742. * and if there is not button press without
  743. * release
  744. */
  745. if ((mbhc->current_plug !=
  746. MBHC_PLUG_TYPE_HEADSET) &&
  747. (mbhc->current_plug !=
  748. MBHC_PLUG_TYPE_ANC_HEADPHONE) &&
  749. !wcd_swch_level_remove(mbhc)) {
  750. pr_debug("%s: cable is %s headset\n",
  751. __func__,
  752. ((spl_hs_count ==
  753. WCD_MBHC_SPL_HS_CNT) ?
  754. "special ":""));
  755. goto report;
  756. }
  757. }
  758. wrk_complete = false;
  759. }
  760. }
  761. if (!wrk_complete) {
  762. /*
  763. * If plug_tye is headset, we might have already reported either
  764. * in detect_plug-type or in above while loop, no need to report
  765. * again
  766. */
  767. if ((plug_type == MBHC_PLUG_TYPE_HEADSET) ||
  768. (plug_type == MBHC_PLUG_TYPE_ANC_HEADPHONE)) {
  769. pr_debug("%s: plug_type:0x%x current_plug: 0x%x already reported\n",
  770. __func__, plug_type, mbhc->current_plug);
  771. if (mbhc->current_plug != plug_type)
  772. goto report;
  773. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_MODE, 0);
  774. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_EN, 0);
  775. goto enable_supply;
  776. }
  777. }
  778. if (plug_type == MBHC_PLUG_TYPE_HIGH_HPH) {
  779. if (wcd_is_special_headset(mbhc)) {
  780. pr_debug("%s: Special headset found %d\n",
  781. __func__, plug_type);
  782. plug_type = MBHC_PLUG_TYPE_HEADSET;
  783. } else {
  784. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_ISRC_EN, 1);
  785. }
  786. }
  787. report:
  788. if (wcd_swch_level_remove(mbhc)) {
  789. pr_debug("%s: Switch level is low\n", __func__);
  790. goto exit;
  791. }
  792. pr_debug("%s: Valid plug found, plug type %d wrk_cmpt %d btn_intr %d\n",
  793. __func__, plug_type, wrk_complete,
  794. mbhc->btn_press_intr);
  795. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_MODE, 0);
  796. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_EN, 0);
  797. WCD_MBHC_RSC_LOCK(mbhc);
  798. wcd_mbhc_find_plug_and_report(mbhc, plug_type);
  799. WCD_MBHC_RSC_UNLOCK(mbhc);
  800. enable_supply:
  801. /*
  802. * Set DETECTION_DONE bit for HEADSET and ANC_HEADPHONE,
  803. * so that btn press/release interrupt can be generated.
  804. * For other plug type, clear the bit.
  805. */
  806. if (plug_type == MBHC_PLUG_TYPE_HEADSET ||
  807. plug_type == MBHC_PLUG_TYPE_ANC_HEADPHONE)
  808. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_DETECTION_DONE, 1);
  809. else
  810. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_DETECTION_DONE, 0);
  811. if (mbhc->mbhc_cb->bcs_enable)
  812. mbhc->mbhc_cb->bcs_enable(mbhc, true);
  813. if (mbhc->mbhc_cb->mbhc_micbias_control)
  814. wcd_mbhc_adc_update_fsm_source(mbhc, plug_type);
  815. exit:
  816. if (mbhc->mbhc_cb->mbhc_micbias_control &&
  817. !mbhc->micbias_enable)
  818. mbhc->mbhc_cb->mbhc_micbias_control(component, MIC_BIAS_2,
  819. MICB_DISABLE);
  820. /*
  821. * If plug type is corrected from special headset to headphone,
  822. * clear the micbias enable flag, set micbias back to 1.8V and
  823. * disable micbias.
  824. */
  825. if (plug_type == MBHC_PLUG_TYPE_HEADPHONE &&
  826. mbhc->micbias_enable) {
  827. if (mbhc->mbhc_cb->mbhc_micbias_control)
  828. mbhc->mbhc_cb->mbhc_micbias_control(
  829. component, MIC_BIAS_2,
  830. MICB_DISABLE);
  831. if (mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic)
  832. mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic(
  833. component,
  834. MIC_BIAS_2, false);
  835. if (mbhc->mbhc_cb->set_micbias_value) {
  836. mbhc->mbhc_cb->set_micbias_value(component);
  837. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MICB_CTRL, 0);
  838. }
  839. mbhc->micbias_enable = false;
  840. }
  841. if (mbhc->mbhc_cfg->detect_extn_cable &&
  842. ((plug_type == MBHC_PLUG_TYPE_HEADPHONE) ||
  843. (plug_type == MBHC_PLUG_TYPE_HEADSET)) &&
  844. !mbhc->hs_detect_work_stop) {
  845. WCD_MBHC_RSC_LOCK(mbhc);
  846. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_REM, true);
  847. WCD_MBHC_RSC_UNLOCK(mbhc);
  848. }
  849. /*
  850. * Enable ADC COMPLETE interrupt for HEADPHONE.
  851. * Btn release may happen after the correct work, ADC COMPLETE
  852. * interrupt needs to be captured to correct plug type.
  853. */
  854. if (plug_type == MBHC_PLUG_TYPE_HEADPHONE) {
  855. WCD_MBHC_RSC_LOCK(mbhc);
  856. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS,
  857. true);
  858. WCD_MBHC_RSC_UNLOCK(mbhc);
  859. }
  860. if (mbhc->mbhc_cb->hph_pull_down_ctrl)
  861. mbhc->mbhc_cb->hph_pull_down_ctrl(component, true);
  862. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  863. pr_debug("%s: leave\n", __func__);
  864. }
  865. static irqreturn_t wcd_mbhc_adc_hs_rem_irq(int irq, void *data)
  866. {
  867. struct wcd_mbhc *mbhc = data;
  868. unsigned long timeout;
  869. int adc_threshold, output_mv, retry = 0;
  870. bool hphpa_on = false;
  871. u8 moisture_status = 0;
  872. pr_debug("%s: enter\n", __func__);
  873. WCD_MBHC_RSC_LOCK(mbhc);
  874. timeout = jiffies +
  875. msecs_to_jiffies(WCD_FAKE_REMOVAL_MIN_PERIOD_MS);
  876. adc_threshold = wcd_mbhc_adc_get_hs_thres(mbhc);
  877. /* Enable MICBIAS before checking for ADC Voltage */
  878. if (mbhc->mbhc_cb->mbhc_micbias_control)
  879. mbhc->mbhc_cb->mbhc_micbias_control(mbhc->component,
  880. MIC_BIAS_2, MICB_ENABLE);
  881. do {
  882. retry++;
  883. /*
  884. * read output_mv every 10ms to look for
  885. * any change in IN2_P
  886. */
  887. usleep_range(10000, 10100);
  888. output_mv = wcd_measure_adc_once(mbhc, MUX_CTL_IN2P);
  889. pr_debug("%s: Check for fake removal: output_mv %d\n",
  890. __func__, output_mv);
  891. if ((output_mv <= adc_threshold) &&
  892. retry > FAKE_REM_RETRY_ATTEMPTS) {
  893. pr_debug("%s: headset is NOT actually removed\n",
  894. __func__);
  895. if (mbhc->mbhc_cb->mbhc_micbias_control)
  896. mbhc->mbhc_cb->mbhc_micbias_control(
  897. mbhc->component, MIC_BIAS_2,
  898. MICB_DISABLE);
  899. goto exit;
  900. }
  901. } while (!time_after(jiffies, timeout));
  902. if (mbhc->mbhc_cb->mbhc_micbias_control)
  903. mbhc->mbhc_cb->mbhc_micbias_control(mbhc->component, MIC_BIAS_2,
  904. MICB_DISABLE);
  905. if (wcd_swch_level_remove(mbhc)) {
  906. pr_debug("%s: Switch level is low ", __func__);
  907. goto exit;
  908. }
  909. if (mbhc->mbhc_cfg->moisture_en ||
  910. mbhc->mbhc_cfg->moisture_duty_cycle_en) {
  911. if (mbhc->mbhc_cb->hph_pa_on_status)
  912. if (mbhc->mbhc_cb->hph_pa_on_status(mbhc->component)) {
  913. hphpa_on = true;
  914. WCD_MBHC_REG_UPDATE_BITS(
  915. WCD_MBHC_HPH_PA_EN, 0);
  916. }
  917. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHR_GND, 1);
  918. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHL_GND, 1);
  919. /* wait for 50ms to get moisture status */
  920. usleep_range(50000, 50100);
  921. WCD_MBHC_REG_READ(WCD_MBHC_MOISTURE_STATUS, moisture_status);
  922. }
  923. if ((mbhc->mbhc_cfg->moisture_en ||
  924. mbhc->mbhc_cfg->moisture_duty_cycle_en) && !moisture_status) {
  925. pr_debug("%s: moisture present in jack\n", __func__);
  926. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 0);
  927. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MECH_DETECTION_TYPE, 1);
  928. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 1);
  929. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  930. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  931. mbhc->btn_press_intr = false;
  932. mbhc->is_btn_press = false;
  933. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADSET) {
  934. wcd_mbhc_report_plug(mbhc, 0, SND_JACK_HEADSET);
  935. extcon_set_state_sync(mbhc->extdev, EXTCON_JACK_MICROPHONE, 0);
  936. } else if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADPHONE) {
  937. wcd_mbhc_report_plug(mbhc, 0, SND_JACK_HEADPHONE);
  938. extcon_set_state_sync(mbhc->extdev, EXTCON_JACK_HEADPHONE, 0);
  939. } else if (mbhc->current_plug == MBHC_PLUG_TYPE_GND_MIC_SWAP) {
  940. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  941. wcd_mbhc_report_plug(mbhc, 0, SND_JACK_UNSUPPORTED);
  942. #endif /* CONFIG_AUDIO_QGKI */
  943. extcon_set_state_sync(mbhc->extdev, EXTCON_MECHANICAL, 0);
  944. } else if (mbhc->current_plug == MBHC_PLUG_TYPE_HIGH_HPH) {
  945. wcd_mbhc_report_plug(mbhc, 0, SND_JACK_LINEOUT);
  946. extcon_set_state_sync(mbhc->extdev, EXTCON_JACK_LINE_OUT, 0);
  947. }
  948. } else {
  949. /*
  950. * ADC COMPLETE and ELEC_REM interrupts are both enabled for
  951. * HEADPHONE, need to reject the ADC COMPLETE interrupt which
  952. * follows ELEC_REM one when HEADPHONE is removed.
  953. */
  954. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADPHONE)
  955. mbhc->extn_cable_hph_rem = true;
  956. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_DETECTION_DONE, 0);
  957. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_MODE, 0);
  958. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_EN, 0);
  959. wcd_mbhc_elec_hs_report_unplug(mbhc);
  960. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  961. if (hphpa_on) {
  962. hphpa_on = false;
  963. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPH_PA_EN, 3);
  964. }
  965. }
  966. exit:
  967. WCD_MBHC_RSC_UNLOCK(mbhc);
  968. pr_debug("%s: leave\n", __func__);
  969. return IRQ_HANDLED;
  970. }
  971. static irqreturn_t wcd_mbhc_adc_hs_ins_irq(int irq, void *data)
  972. {
  973. struct wcd_mbhc *mbhc = data;
  974. u8 clamp_state = 0;
  975. u8 clamp_retry = WCD_MBHC_FAKE_INS_RETRY;
  976. pr_debug("%s: enter\n", __func__);
  977. /*
  978. * ADC COMPLETE and ELEC_REM interrupts are both enabled for HEADPHONE,
  979. * need to reject the ADC COMPLETE interrupt which follows ELEC_REM one
  980. * when HEADPHONE is removed.
  981. */
  982. if (mbhc->extn_cable_hph_rem == true) {
  983. mbhc->extn_cable_hph_rem = false;
  984. pr_debug("%s: leave\n", __func__);
  985. return IRQ_HANDLED;
  986. }
  987. do {
  988. WCD_MBHC_REG_READ(WCD_MBHC_IN2P_CLAMP_STATE, clamp_state);
  989. if (clamp_state) {
  990. pr_debug("%s: fake insertion irq, leave\n", __func__);
  991. return IRQ_HANDLED;
  992. }
  993. /*
  994. * check clamp for 120ms but at 30ms chunks to leave
  995. * room for other interrupts to be processed
  996. */
  997. usleep_range(30000, 30100);
  998. } while (--clamp_retry);
  999. WCD_MBHC_RSC_LOCK(mbhc);
  1000. if (!(test_bit(WCD_MBHC_ELEC_HS_INS, &mbhc->intr_status))) {
  1001. WCD_MBHC_RSC_UNLOCK(mbhc);
  1002. return IRQ_HANDLED;
  1003. }
  1004. /*
  1005. * If current plug is headphone then there is no chance to
  1006. * get ADC complete interrupt, so connected cable should be
  1007. * headset not headphone.
  1008. */
  1009. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADPHONE) {
  1010. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS, false);
  1011. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_DETECTION_DONE, 1);
  1012. wcd_mbhc_find_plug_and_report(mbhc, MBHC_PLUG_TYPE_HEADSET);
  1013. WCD_MBHC_RSC_UNLOCK(mbhc);
  1014. return IRQ_HANDLED;
  1015. }
  1016. if (!mbhc->mbhc_cfg->detect_extn_cable) {
  1017. pr_debug("%s: Returning as Extension cable feature not enabled\n",
  1018. __func__);
  1019. WCD_MBHC_RSC_UNLOCK(mbhc);
  1020. return IRQ_HANDLED;
  1021. }
  1022. pr_debug("%s: Disable electrical headset insertion interrupt\n",
  1023. __func__);
  1024. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS, false);
  1025. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_SCHMT_ISRC, 0);
  1026. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_ISRC_EN, 0);
  1027. mbhc->is_extn_cable = true;
  1028. mbhc->btn_press_intr = false;
  1029. mbhc->force_linein = false;
  1030. wcd_mbhc_adc_detect_plug_type(mbhc);
  1031. WCD_MBHC_RSC_UNLOCK(mbhc);
  1032. pr_debug("%s: leave\n", __func__);
  1033. return IRQ_HANDLED;
  1034. }
  1035. static struct wcd_mbhc_fn mbhc_fn = {
  1036. .wcd_mbhc_hs_ins_irq = wcd_mbhc_adc_hs_ins_irq,
  1037. .wcd_mbhc_hs_rem_irq = wcd_mbhc_adc_hs_rem_irq,
  1038. .wcd_mbhc_detect_plug_type = wcd_mbhc_adc_detect_plug_type,
  1039. .wcd_mbhc_detect_anc_plug_type = wcd_mbhc_adc_detect_anc_plug_type,
  1040. .wcd_cancel_hs_detect_plug = wcd_cancel_hs_detect_plug,
  1041. };
  1042. /* Function: wcd_mbhc_adc_init
  1043. * @mbhc: MBHC function pointer
  1044. * Description: Initialize MBHC ADC related function pointers to MBHC structure
  1045. */
  1046. void wcd_mbhc_adc_init(struct wcd_mbhc *mbhc)
  1047. {
  1048. if (!mbhc) {
  1049. pr_err("%s: mbhc is NULL\n", __func__);
  1050. return;
  1051. }
  1052. mbhc->mbhc_fn = &mbhc_fn;
  1053. INIT_WORK(&mbhc->correct_plug_swch, wcd_correct_swch_plug);
  1054. }
  1055. EXPORT_SYMBOL(wcd_mbhc_adc_init);