wcd-mbhc-adc.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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 current_plug: 0x%x already reported\n",
  774. __func__, plug_type, mbhc->current_plug);
  775. if (mbhc->current_plug != plug_type)
  776. goto report;
  777. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_MODE, 0);
  778. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_EN, 0);
  779. goto enable_supply;
  780. }
  781. }
  782. if (plug_type == MBHC_PLUG_TYPE_HIGH_HPH) {
  783. if (wcd_is_special_headset(mbhc)) {
  784. pr_debug("%s: Special headset found %d\n",
  785. __func__, plug_type);
  786. plug_type = MBHC_PLUG_TYPE_HEADSET;
  787. } else {
  788. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_ISRC_EN, 1);
  789. }
  790. }
  791. report:
  792. if (wcd_swch_level_remove(mbhc)) {
  793. pr_debug("%s: Switch level is low\n", __func__);
  794. goto exit;
  795. }
  796. pr_debug("%s: Valid plug found, plug type %d wrk_cmpt %d btn_intr %d\n",
  797. __func__, plug_type, wrk_complete,
  798. mbhc->btn_press_intr);
  799. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_MODE, 0);
  800. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_EN, 0);
  801. WCD_MBHC_RSC_LOCK(mbhc);
  802. wcd_mbhc_find_plug_and_report(mbhc, plug_type);
  803. WCD_MBHC_RSC_UNLOCK(mbhc);
  804. enable_supply:
  805. /*
  806. * Set DETECTION_DONE bit for HEADSET and ANC_HEADPHONE,
  807. * so that btn press/release interrupt can be generated.
  808. * For other plug type, clear the bit.
  809. */
  810. if (plug_type == MBHC_PLUG_TYPE_HEADSET ||
  811. plug_type == MBHC_PLUG_TYPE_ANC_HEADPHONE)
  812. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_DETECTION_DONE, 1);
  813. else
  814. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_DETECTION_DONE, 0);
  815. if (mbhc->mbhc_cb->mbhc_micbias_control)
  816. wcd_mbhc_adc_update_fsm_source(mbhc, plug_type);
  817. exit:
  818. if (mbhc->mbhc_cb->mbhc_micbias_control &&
  819. !mbhc->micbias_enable)
  820. mbhc->mbhc_cb->mbhc_micbias_control(component, MIC_BIAS_2,
  821. MICB_DISABLE);
  822. /*
  823. * If plug type is corrected from special headset to headphone,
  824. * clear the micbias enable flag, set micbias back to 1.8V and
  825. * disable micbias.
  826. */
  827. if (plug_type == MBHC_PLUG_TYPE_HEADPHONE &&
  828. mbhc->micbias_enable) {
  829. if (mbhc->mbhc_cb->mbhc_micbias_control)
  830. mbhc->mbhc_cb->mbhc_micbias_control(
  831. component, MIC_BIAS_2,
  832. MICB_DISABLE);
  833. if (mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic)
  834. mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic(
  835. component,
  836. MIC_BIAS_2, false);
  837. if (mbhc->mbhc_cb->set_micbias_value) {
  838. mbhc->mbhc_cb->set_micbias_value(component);
  839. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MICB_CTRL, 0);
  840. }
  841. mbhc->micbias_enable = false;
  842. }
  843. if (mbhc->mbhc_cfg->detect_extn_cable &&
  844. ((plug_type == MBHC_PLUG_TYPE_HEADPHONE) ||
  845. (plug_type == MBHC_PLUG_TYPE_HEADSET)) &&
  846. !mbhc->hs_detect_work_stop) {
  847. WCD_MBHC_RSC_LOCK(mbhc);
  848. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_REM, true);
  849. WCD_MBHC_RSC_UNLOCK(mbhc);
  850. }
  851. /*
  852. * Enable ADC COMPLETE interrupt for HEADPHONE.
  853. * Btn release may happen after the correct work, ADC COMPLETE
  854. * interrupt needs to be captured to correct plug type.
  855. */
  856. if (plug_type == MBHC_PLUG_TYPE_HEADPHONE) {
  857. WCD_MBHC_RSC_LOCK(mbhc);
  858. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS,
  859. true);
  860. WCD_MBHC_RSC_UNLOCK(mbhc);
  861. }
  862. if (mbhc->mbhc_cb->hph_pull_down_ctrl)
  863. mbhc->mbhc_cb->hph_pull_down_ctrl(component, true);
  864. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  865. pr_debug("%s: leave\n", __func__);
  866. }
  867. static irqreturn_t wcd_mbhc_adc_hs_rem_irq(int irq, void *data)
  868. {
  869. struct wcd_mbhc *mbhc = data;
  870. unsigned long timeout;
  871. int adc_threshold, output_mv, retry = 0;
  872. bool hphpa_on = false;
  873. u8 moisture_status = 0;
  874. pr_debug("%s: enter\n", __func__);
  875. WCD_MBHC_RSC_LOCK(mbhc);
  876. timeout = jiffies +
  877. msecs_to_jiffies(WCD_FAKE_REMOVAL_MIN_PERIOD_MS);
  878. adc_threshold = wcd_mbhc_adc_get_hs_thres(mbhc);
  879. do {
  880. retry++;
  881. /*
  882. * read output_mv every 10ms to look for
  883. * any change in IN2_P
  884. */
  885. usleep_range(10000, 10100);
  886. output_mv = wcd_measure_adc_once(mbhc, MUX_CTL_IN2P);
  887. pr_debug("%s: Check for fake removal: output_mv %d\n",
  888. __func__, output_mv);
  889. if ((output_mv <= adc_threshold) &&
  890. retry > FAKE_REM_RETRY_ATTEMPTS) {
  891. pr_debug("%s: headset is NOT actually removed\n",
  892. __func__);
  893. goto exit;
  894. }
  895. } while (!time_after(jiffies, timeout));
  896. if (wcd_swch_level_remove(mbhc)) {
  897. pr_debug("%s: Switch level is low ", __func__);
  898. goto exit;
  899. }
  900. if (mbhc->mbhc_cfg->moisture_en) {
  901. if (mbhc->mbhc_cb->hph_pa_on_status)
  902. if (mbhc->mbhc_cb->hph_pa_on_status(mbhc->component)) {
  903. hphpa_on = true;
  904. WCD_MBHC_REG_UPDATE_BITS(
  905. WCD_MBHC_HPH_PA_EN, 0);
  906. }
  907. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHR_GND, 1);
  908. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHL_GND, 1);
  909. /* wait for 50ms to get moisture status */
  910. usleep_range(50000, 50100);
  911. WCD_MBHC_REG_READ(WCD_MBHC_MOISTURE_STATUS, moisture_status);
  912. }
  913. if (mbhc->mbhc_cfg->moisture_en && !moisture_status) {
  914. pr_debug("%s: moisture present in jack\n", __func__);
  915. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 0);
  916. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MECH_DETECTION_TYPE, 1);
  917. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 1);
  918. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  919. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  920. mbhc->btn_press_intr = false;
  921. mbhc->is_btn_press = false;
  922. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADSET)
  923. wcd_mbhc_report_plug(mbhc, 0, SND_JACK_HEADSET);
  924. else if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADPHONE)
  925. wcd_mbhc_report_plug(mbhc, 0, SND_JACK_HEADPHONE);
  926. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  927. else if (mbhc->current_plug == MBHC_PLUG_TYPE_GND_MIC_SWAP)
  928. wcd_mbhc_report_plug(mbhc, 0, SND_JACK_UNSUPPORTED);
  929. #endif /* CONFIG_AUDIO_QGKI */
  930. else if (mbhc->current_plug == MBHC_PLUG_TYPE_HIGH_HPH)
  931. wcd_mbhc_report_plug(mbhc, 0, SND_JACK_LINEOUT);
  932. } else {
  933. /*
  934. * ADC COMPLETE and ELEC_REM interrupts are both enabled for
  935. * HEADPHONE, need to reject the ADC COMPLETE interrupt which
  936. * follows ELEC_REM one when HEADPHONE is removed.
  937. */
  938. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADPHONE)
  939. mbhc->extn_cable_hph_rem = true;
  940. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_DETECTION_DONE, 0);
  941. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_MODE, 0);
  942. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_EN, 0);
  943. wcd_mbhc_elec_hs_report_unplug(mbhc);
  944. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  945. if (hphpa_on) {
  946. hphpa_on = false;
  947. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPH_PA_EN, 3);
  948. }
  949. }
  950. exit:
  951. WCD_MBHC_RSC_UNLOCK(mbhc);
  952. pr_debug("%s: leave\n", __func__);
  953. return IRQ_HANDLED;
  954. }
  955. static irqreturn_t wcd_mbhc_adc_hs_ins_irq(int irq, void *data)
  956. {
  957. struct wcd_mbhc *mbhc = data;
  958. u8 clamp_state = 0;
  959. u8 clamp_retry = WCD_MBHC_FAKE_INS_RETRY;
  960. pr_debug("%s: enter\n", __func__);
  961. /*
  962. * ADC COMPLETE and ELEC_REM interrupts are both enabled for HEADPHONE,
  963. * need to reject the ADC COMPLETE interrupt which follows ELEC_REM one
  964. * when HEADPHONE is removed.
  965. */
  966. if (mbhc->extn_cable_hph_rem == true) {
  967. mbhc->extn_cable_hph_rem = false;
  968. pr_debug("%s: leave\n", __func__);
  969. return IRQ_HANDLED;
  970. }
  971. do {
  972. WCD_MBHC_REG_READ(WCD_MBHC_IN2P_CLAMP_STATE, clamp_state);
  973. if (clamp_state) {
  974. pr_debug("%s: fake insertion irq, leave\n", __func__);
  975. return IRQ_HANDLED;
  976. }
  977. /*
  978. * check clamp for 120ms but at 30ms chunks to leave
  979. * room for other interrupts to be processed
  980. */
  981. usleep_range(30000, 30100);
  982. } while (--clamp_retry);
  983. WCD_MBHC_RSC_LOCK(mbhc);
  984. /*
  985. * If current plug is headphone then there is no chance to
  986. * get ADC complete interrupt, so connected cable should be
  987. * headset not headphone.
  988. */
  989. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADPHONE) {
  990. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS, false);
  991. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_DETECTION_DONE, 1);
  992. wcd_mbhc_find_plug_and_report(mbhc, MBHC_PLUG_TYPE_HEADSET);
  993. WCD_MBHC_RSC_UNLOCK(mbhc);
  994. return IRQ_HANDLED;
  995. }
  996. if (!mbhc->mbhc_cfg->detect_extn_cable) {
  997. pr_debug("%s: Returning as Extension cable feature not enabled\n",
  998. __func__);
  999. WCD_MBHC_RSC_UNLOCK(mbhc);
  1000. return IRQ_HANDLED;
  1001. }
  1002. pr_debug("%s: Disable electrical headset insertion interrupt\n",
  1003. __func__);
  1004. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS, false);
  1005. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_SCHMT_ISRC, 0);
  1006. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_ISRC_EN, 0);
  1007. mbhc->is_extn_cable = true;
  1008. mbhc->btn_press_intr = false;
  1009. mbhc->force_linein = false;
  1010. wcd_mbhc_adc_detect_plug_type(mbhc);
  1011. WCD_MBHC_RSC_UNLOCK(mbhc);
  1012. pr_debug("%s: leave\n", __func__);
  1013. return IRQ_HANDLED;
  1014. }
  1015. static struct wcd_mbhc_fn mbhc_fn = {
  1016. .wcd_mbhc_hs_ins_irq = wcd_mbhc_adc_hs_ins_irq,
  1017. .wcd_mbhc_hs_rem_irq = wcd_mbhc_adc_hs_rem_irq,
  1018. .wcd_mbhc_detect_plug_type = wcd_mbhc_adc_detect_plug_type,
  1019. .wcd_mbhc_detect_anc_plug_type = wcd_mbhc_adc_detect_anc_plug_type,
  1020. .wcd_cancel_hs_detect_plug = wcd_cancel_hs_detect_plug,
  1021. };
  1022. /* Function: wcd_mbhc_adc_init
  1023. * @mbhc: MBHC function pointer
  1024. * Description: Initialize MBHC ADC related function pointers to MBHC structure
  1025. */
  1026. void wcd_mbhc_adc_init(struct wcd_mbhc *mbhc)
  1027. {
  1028. if (!mbhc) {
  1029. pr_err("%s: mbhc is NULL\n", __func__);
  1030. return;
  1031. }
  1032. mbhc->mbhc_fn = &mbhc_fn;
  1033. INIT_WORK(&mbhc->correct_plug_swch, wcd_correct_swch_plug);
  1034. }
  1035. EXPORT_SYMBOL(wcd_mbhc_adc_init);