wcd-mbhc-adc.c 33 KB

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