wcd-mbhc-adc.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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 (!(test_bit(WCD_MBHC_ELEC_HS_REM, &mbhc->intr_status))) {
  910. pr_debug("%s: plug removal already reported.\n", __func__);
  911. goto exit;
  912. }
  913. if (mbhc->mbhc_cfg->moisture_en ||
  914. mbhc->mbhc_cfg->moisture_duty_cycle_en) {
  915. if (mbhc->mbhc_cb->hph_pa_on_status)
  916. if (mbhc->mbhc_cb->hph_pa_on_status(mbhc->component)) {
  917. hphpa_on = true;
  918. WCD_MBHC_REG_UPDATE_BITS(
  919. WCD_MBHC_HPH_PA_EN, 0);
  920. }
  921. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHR_GND, 1);
  922. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHL_GND, 1);
  923. /* wait for 50ms to get moisture status */
  924. usleep_range(50000, 50100);
  925. WCD_MBHC_REG_READ(WCD_MBHC_MOISTURE_STATUS, moisture_status);
  926. }
  927. if ((mbhc->mbhc_cfg->moisture_en ||
  928. mbhc->mbhc_cfg->moisture_duty_cycle_en) && !moisture_status) {
  929. pr_debug("%s: moisture present in jack\n", __func__);
  930. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 0);
  931. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MECH_DETECTION_TYPE, 1);
  932. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 1);
  933. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  934. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  935. mbhc->btn_press_intr = false;
  936. mbhc->is_btn_press = false;
  937. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADSET) {
  938. wcd_mbhc_report_plug(mbhc, 0, SND_JACK_HEADSET);
  939. extcon_set_state_sync(mbhc->extdev, EXTCON_JACK_MICROPHONE, 0);
  940. } else if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADPHONE) {
  941. wcd_mbhc_report_plug(mbhc, 0, SND_JACK_HEADPHONE);
  942. extcon_set_state_sync(mbhc->extdev, EXTCON_JACK_HEADPHONE, 0);
  943. } else if (mbhc->current_plug == MBHC_PLUG_TYPE_GND_MIC_SWAP) {
  944. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  945. wcd_mbhc_report_plug(mbhc, 0, SND_JACK_UNSUPPORTED);
  946. #endif /* CONFIG_AUDIO_QGKI */
  947. extcon_set_state_sync(mbhc->extdev, EXTCON_MECHANICAL, 0);
  948. } else if (mbhc->current_plug == MBHC_PLUG_TYPE_HIGH_HPH) {
  949. wcd_mbhc_report_plug(mbhc, 0, SND_JACK_LINEOUT);
  950. extcon_set_state_sync(mbhc->extdev, EXTCON_JACK_LINE_OUT, 0);
  951. }
  952. } else {
  953. /*
  954. * ADC COMPLETE and ELEC_REM interrupts are both enabled for
  955. * HEADPHONE, need to reject the ADC COMPLETE interrupt which
  956. * follows ELEC_REM one when HEADPHONE is removed.
  957. */
  958. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADPHONE)
  959. mbhc->extn_cable_hph_rem = true;
  960. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_DETECTION_DONE, 0);
  961. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_MODE, 0);
  962. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_EN, 0);
  963. wcd_mbhc_elec_hs_report_unplug(mbhc);
  964. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  965. if (hphpa_on) {
  966. hphpa_on = false;
  967. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPH_PA_EN, 3);
  968. }
  969. }
  970. exit:
  971. WCD_MBHC_RSC_UNLOCK(mbhc);
  972. pr_debug("%s: leave\n", __func__);
  973. return IRQ_HANDLED;
  974. }
  975. static irqreturn_t wcd_mbhc_adc_hs_ins_irq(int irq, void *data)
  976. {
  977. struct wcd_mbhc *mbhc = data;
  978. u8 clamp_state = 0;
  979. u8 clamp_retry = WCD_MBHC_FAKE_INS_RETRY;
  980. pr_debug("%s: enter\n", __func__);
  981. /*
  982. * ADC COMPLETE and ELEC_REM interrupts are both enabled for HEADPHONE,
  983. * need to reject the ADC COMPLETE interrupt which follows ELEC_REM one
  984. * when HEADPHONE is removed.
  985. */
  986. if (mbhc->extn_cable_hph_rem == true) {
  987. mbhc->extn_cable_hph_rem = false;
  988. pr_debug("%s: leave\n", __func__);
  989. return IRQ_HANDLED;
  990. }
  991. do {
  992. WCD_MBHC_REG_READ(WCD_MBHC_IN2P_CLAMP_STATE, clamp_state);
  993. if (clamp_state) {
  994. pr_debug("%s: fake insertion irq, leave\n", __func__);
  995. return IRQ_HANDLED;
  996. }
  997. /*
  998. * check clamp for 120ms but at 30ms chunks to leave
  999. * room for other interrupts to be processed
  1000. */
  1001. usleep_range(30000, 30100);
  1002. } while (--clamp_retry);
  1003. WCD_MBHC_RSC_LOCK(mbhc);
  1004. if (!(test_bit(WCD_MBHC_ELEC_HS_INS, &mbhc->intr_status))) {
  1005. WCD_MBHC_RSC_UNLOCK(mbhc);
  1006. return IRQ_HANDLED;
  1007. }
  1008. /*
  1009. * If current plug is headphone then there is no chance to
  1010. * get ADC complete interrupt, so connected cable should be
  1011. * headset not headphone.
  1012. */
  1013. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADPHONE) {
  1014. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS, false);
  1015. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_DETECTION_DONE, 1);
  1016. wcd_mbhc_find_plug_and_report(mbhc, MBHC_PLUG_TYPE_HEADSET);
  1017. WCD_MBHC_RSC_UNLOCK(mbhc);
  1018. return IRQ_HANDLED;
  1019. }
  1020. if (!mbhc->mbhc_cfg->detect_extn_cable) {
  1021. pr_debug("%s: Returning as Extension cable feature not enabled\n",
  1022. __func__);
  1023. WCD_MBHC_RSC_UNLOCK(mbhc);
  1024. return IRQ_HANDLED;
  1025. }
  1026. pr_debug("%s: Disable electrical headset insertion interrupt\n",
  1027. __func__);
  1028. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS, false);
  1029. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_SCHMT_ISRC, 0);
  1030. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_ISRC_EN, 0);
  1031. mbhc->is_extn_cable = true;
  1032. mbhc->btn_press_intr = false;
  1033. mbhc->force_linein = false;
  1034. wcd_mbhc_adc_detect_plug_type(mbhc);
  1035. WCD_MBHC_RSC_UNLOCK(mbhc);
  1036. pr_debug("%s: leave\n", __func__);
  1037. return IRQ_HANDLED;
  1038. }
  1039. static struct wcd_mbhc_fn mbhc_fn = {
  1040. .wcd_mbhc_hs_ins_irq = wcd_mbhc_adc_hs_ins_irq,
  1041. .wcd_mbhc_hs_rem_irq = wcd_mbhc_adc_hs_rem_irq,
  1042. .wcd_mbhc_detect_plug_type = wcd_mbhc_adc_detect_plug_type,
  1043. .wcd_mbhc_detect_anc_plug_type = wcd_mbhc_adc_detect_anc_plug_type,
  1044. .wcd_cancel_hs_detect_plug = wcd_cancel_hs_detect_plug,
  1045. };
  1046. /* Function: wcd_mbhc_adc_init
  1047. * @mbhc: MBHC function pointer
  1048. * Description: Initialize MBHC ADC related function pointers to MBHC structure
  1049. */
  1050. void wcd_mbhc_adc_init(struct wcd_mbhc *mbhc)
  1051. {
  1052. if (!mbhc) {
  1053. pr_err("%s: mbhc is NULL\n", __func__);
  1054. return;
  1055. }
  1056. mbhc->mbhc_fn = &mbhc_fn;
  1057. INIT_WORK(&mbhc->correct_plug_swch, wcd_correct_swch_plug);
  1058. }
  1059. EXPORT_SYMBOL(wcd_mbhc_adc_init);