wcd-mbhc-adc.c 31 KB

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