wcd-mbhc-adc.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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->component,
  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->component,
  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->component))
  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_component *component = mbhc->component;
  296. struct wcd9xxx_pdata *pdata = dev_get_platdata(component->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->component,
  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->component,
  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_component *component = mbhc->component;
  349. struct wcd9xxx_pdata *pdata = dev_get_platdata(component->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->component,
  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->component,
  404. MIC_BIAS_2,
  405. false);
  406. pr_debug("%s: leave, micb_enable: %d\n", __func__,
  407. mbhc->micbias_enable);
  408. return ret;
  409. }
  410. static void wcd_mbhc_adc_update_fsm_source(struct wcd_mbhc *mbhc,
  411. enum wcd_mbhc_plug_type plug_type)
  412. {
  413. bool micbias2;
  414. micbias2 = mbhc->mbhc_cb->micbias_enable_status(mbhc,
  415. MIC_BIAS_2);
  416. switch (plug_type) {
  417. case MBHC_PLUG_TYPE_HEADPHONE:
  418. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 3);
  419. break;
  420. case MBHC_PLUG_TYPE_HEADSET:
  421. case MBHC_PLUG_TYPE_ANC_HEADPHONE:
  422. if (!mbhc->is_hs_recording && !micbias2)
  423. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 3);
  424. break;
  425. default:
  426. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  427. break;
  428. };
  429. }
  430. /* should be called under interrupt context that hold suspend */
  431. static void wcd_schedule_hs_detect_plug(struct wcd_mbhc *mbhc,
  432. struct work_struct *work)
  433. {
  434. pr_debug("%s: scheduling correct_swch_plug\n", __func__);
  435. WCD_MBHC_RSC_ASSERT_LOCKED(mbhc);
  436. mbhc->hs_detect_work_stop = false;
  437. mbhc->mbhc_cb->lock_sleep(mbhc, true);
  438. schedule_work(work);
  439. }
  440. /* called under codec_resource_lock acquisition */
  441. static void wcd_cancel_hs_detect_plug(struct wcd_mbhc *mbhc,
  442. struct work_struct *work)
  443. {
  444. pr_debug("%s: Canceling correct_plug_swch\n", __func__);
  445. mbhc->hs_detect_work_stop = true;
  446. WCD_MBHC_RSC_UNLOCK(mbhc);
  447. if (cancel_work_sync(work)) {
  448. pr_debug("%s: correct_plug_swch is canceled\n",
  449. __func__);
  450. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  451. }
  452. WCD_MBHC_RSC_LOCK(mbhc);
  453. }
  454. /* called under codec_resource_lock acquisition */
  455. static void wcd_mbhc_adc_detect_plug_type(struct wcd_mbhc *mbhc)
  456. {
  457. struct snd_soc_component *component = mbhc->component;
  458. pr_debug("%s: enter\n", __func__);
  459. WCD_MBHC_RSC_ASSERT_LOCKED(mbhc);
  460. if (mbhc->mbhc_cb->hph_pull_down_ctrl)
  461. mbhc->mbhc_cb->hph_pull_down_ctrl(component, false);
  462. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_DETECTION_DONE, 0);
  463. if (mbhc->mbhc_cb->mbhc_micbias_control) {
  464. mbhc->mbhc_cb->mbhc_micbias_control(component, MIC_BIAS_2,
  465. MICB_ENABLE);
  466. } else {
  467. pr_err("%s: Mic Bias is not enabled\n", __func__);
  468. return;
  469. }
  470. /* Re-initialize button press completion object */
  471. reinit_completion(&mbhc->btn_press_compl);
  472. wcd_schedule_hs_detect_plug(mbhc, &mbhc->correct_plug_swch);
  473. pr_debug("%s: leave\n", __func__);
  474. }
  475. static void wcd_micbias_disable(struct wcd_mbhc *mbhc)
  476. {
  477. if (mbhc->micbias_enable) {
  478. mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic(
  479. mbhc->component, MIC_BIAS_2, false);
  480. if (mbhc->mbhc_cb->set_micbias_value)
  481. mbhc->mbhc_cb->set_micbias_value(
  482. mbhc->component);
  483. mbhc->micbias_enable = false;
  484. }
  485. }
  486. static int wcd_mbhc_get_plug_from_adc(struct wcd_mbhc *mbhc, int adc_result)
  487. {
  488. enum wcd_mbhc_plug_type plug_type = MBHC_PLUG_TYPE_INVALID;
  489. u32 hph_thr = 0, hs_thr = 0;
  490. if (mbhc->hs_thr)
  491. hs_thr = mbhc->hs_thr;
  492. else
  493. hs_thr = WCD_MBHC_ADC_HS_THRESHOLD_MV;
  494. if (mbhc->hph_thr)
  495. hph_thr = mbhc->hph_thr;
  496. else
  497. hph_thr = WCD_MBHC_ADC_HPH_THRESHOLD_MV;
  498. if (adc_result < hph_thr)
  499. plug_type = MBHC_PLUG_TYPE_HEADPHONE;
  500. else if (adc_result > hs_thr)
  501. plug_type = MBHC_PLUG_TYPE_HIGH_HPH;
  502. else
  503. plug_type = MBHC_PLUG_TYPE_HEADSET;
  504. pr_debug("%s: plug type is %d found\n", __func__, plug_type);
  505. return plug_type;
  506. }
  507. static void wcd_correct_swch_plug(struct work_struct *work)
  508. {
  509. struct wcd_mbhc *mbhc;
  510. struct snd_soc_component *component;
  511. enum wcd_mbhc_plug_type plug_type = MBHC_PLUG_TYPE_INVALID;
  512. unsigned long timeout;
  513. bool wrk_complete = false;
  514. int pt_gnd_mic_swap_cnt = 0;
  515. int no_gnd_mic_swap_cnt = 0;
  516. bool is_pa_on = false, spl_hs = false, spl_hs_reported = false;
  517. int ret = 0;
  518. int spl_hs_count = 0;
  519. int output_mv = 0;
  520. int cross_conn;
  521. int try = 0;
  522. pr_debug("%s: enter\n", __func__);
  523. mbhc = container_of(work, struct wcd_mbhc, correct_plug_swch);
  524. component = mbhc->component;
  525. WCD_MBHC_RSC_LOCK(mbhc);
  526. /* Mask ADC COMPLETE interrupt */
  527. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS, false);
  528. WCD_MBHC_RSC_UNLOCK(mbhc);
  529. /* Check for cross connection */
  530. do {
  531. cross_conn = wcd_check_cross_conn(mbhc);
  532. try++;
  533. } while (try < mbhc->swap_thr);
  534. if (cross_conn > 0) {
  535. plug_type = MBHC_PLUG_TYPE_GND_MIC_SWAP;
  536. pr_debug("%s: cross connection found, Plug type %d\n",
  537. __func__, plug_type);
  538. goto correct_plug_type;
  539. }
  540. /* Find plug type */
  541. output_mv = wcd_measure_adc_continuous(mbhc);
  542. plug_type = wcd_mbhc_get_plug_from_adc(mbhc, output_mv);
  543. /*
  544. * Report plug type if it is either headset or headphone
  545. * else start the 3 sec loop
  546. */
  547. if ((plug_type == MBHC_PLUG_TYPE_HEADSET ||
  548. plug_type == MBHC_PLUG_TYPE_HEADPHONE) &&
  549. (!wcd_swch_level_remove(mbhc))) {
  550. WCD_MBHC_RSC_LOCK(mbhc);
  551. wcd_mbhc_find_plug_and_report(mbhc, plug_type);
  552. WCD_MBHC_RSC_UNLOCK(mbhc);
  553. }
  554. /*
  555. * Set DETECTION_DONE bit for HEADSET and ANC_HEADPHONE,
  556. * so that btn press/release interrupt can be generated.
  557. */
  558. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADSET ||
  559. mbhc->current_plug == MBHC_PLUG_TYPE_ANC_HEADPHONE) {
  560. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_MODE, 0);
  561. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_EN, 0);
  562. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_DETECTION_DONE, 1);
  563. }
  564. correct_plug_type:
  565. timeout = jiffies + msecs_to_jiffies(HS_DETECT_PLUG_TIME_MS);
  566. while (!time_after(jiffies, timeout)) {
  567. if (mbhc->hs_detect_work_stop) {
  568. pr_debug("%s: stop requested: %d\n", __func__,
  569. mbhc->hs_detect_work_stop);
  570. wcd_micbias_disable(mbhc);
  571. goto exit;
  572. }
  573. /* allow sometime and re-check stop requested again */
  574. msleep(20);
  575. if (mbhc->hs_detect_work_stop) {
  576. pr_debug("%s: stop requested: %d\n", __func__,
  577. mbhc->hs_detect_work_stop);
  578. wcd_micbias_disable(mbhc);
  579. goto exit;
  580. }
  581. msleep(180);
  582. /*
  583. * Use ADC single mode to minimize the chance of missing out
  584. * btn press/release for HEADSET type during correct work.
  585. */
  586. output_mv = wcd_measure_adc_once(mbhc, MUX_CTL_IN2P);
  587. /*
  588. * instead of hogging system by contineous polling, wait for
  589. * sometime and re-check stop request again.
  590. */
  591. plug_type = wcd_mbhc_get_plug_from_adc(mbhc, output_mv);
  592. if ((output_mv > WCD_MBHC_ADC_HS_THRESHOLD_MV) &&
  593. (spl_hs_count < WCD_MBHC_SPL_HS_CNT)) {
  594. spl_hs = wcd_mbhc_adc_check_for_spl_headset(mbhc,
  595. &spl_hs_count);
  596. if (spl_hs_count == WCD_MBHC_SPL_HS_CNT) {
  597. output_mv = WCD_MBHC_ADC_HS_THRESHOLD_MV;
  598. spl_hs = true;
  599. mbhc->micbias_enable = true;
  600. }
  601. }
  602. if (mbhc->mbhc_cb->hph_pa_on_status)
  603. is_pa_on = mbhc->mbhc_cb->hph_pa_on_status(
  604. mbhc->component);
  605. if ((output_mv <= WCD_MBHC_ADC_HS_THRESHOLD_MV) &&
  606. (!is_pa_on)) {
  607. /* Check for cross connection*/
  608. ret = wcd_check_cross_conn(mbhc);
  609. if (ret < 0)
  610. continue;
  611. else if (ret > 0) {
  612. pt_gnd_mic_swap_cnt++;
  613. no_gnd_mic_swap_cnt = 0;
  614. if (pt_gnd_mic_swap_cnt <
  615. mbhc->swap_thr) {
  616. continue;
  617. } else if (pt_gnd_mic_swap_cnt >
  618. mbhc->swap_thr) {
  619. /*
  620. * This is due to GND/MIC switch didn't
  621. * work, Report unsupported plug.
  622. */
  623. pr_debug("%s: switch did not work\n",
  624. __func__);
  625. plug_type = MBHC_PLUG_TYPE_GND_MIC_SWAP;
  626. goto report;
  627. } else {
  628. plug_type = MBHC_PLUG_TYPE_GND_MIC_SWAP;
  629. }
  630. } else {
  631. no_gnd_mic_swap_cnt++;
  632. pt_gnd_mic_swap_cnt = 0;
  633. plug_type = wcd_mbhc_get_plug_from_adc(
  634. mbhc, output_mv);
  635. if ((no_gnd_mic_swap_cnt <
  636. mbhc->swap_thr) &&
  637. (spl_hs_count != WCD_MBHC_SPL_HS_CNT)) {
  638. continue;
  639. } else {
  640. no_gnd_mic_swap_cnt = 0;
  641. }
  642. }
  643. if ((pt_gnd_mic_swap_cnt == mbhc->swap_thr) &&
  644. (plug_type == MBHC_PLUG_TYPE_GND_MIC_SWAP)) {
  645. /*
  646. * if switch is toggled, check again,
  647. * otherwise report unsupported plug
  648. */
  649. if (mbhc->mbhc_cfg->swap_gnd_mic &&
  650. mbhc->mbhc_cfg->swap_gnd_mic(component,
  651. true)) {
  652. pr_debug("%s: US_EU gpio present,flip switch\n"
  653. , __func__);
  654. continue;
  655. }
  656. }
  657. }
  658. if (output_mv > WCD_MBHC_ADC_HS_THRESHOLD_MV) {
  659. pr_debug("%s: cable is extension cable\n", __func__);
  660. plug_type = MBHC_PLUG_TYPE_HIGH_HPH;
  661. wrk_complete = true;
  662. } else {
  663. pr_debug("%s: cable might be headset: %d\n", __func__,
  664. plug_type);
  665. if (plug_type != MBHC_PLUG_TYPE_GND_MIC_SWAP) {
  666. plug_type = wcd_mbhc_get_plug_from_adc(
  667. mbhc, output_mv);
  668. if (!spl_hs_reported &&
  669. spl_hs_count == WCD_MBHC_SPL_HS_CNT) {
  670. spl_hs_reported = true;
  671. WCD_MBHC_RSC_LOCK(mbhc);
  672. wcd_mbhc_find_plug_and_report(mbhc,
  673. plug_type);
  674. WCD_MBHC_RSC_UNLOCK(mbhc);
  675. continue;
  676. } else if (spl_hs_reported)
  677. continue;
  678. /*
  679. * Report headset only if not already reported
  680. * and if there is not button press without
  681. * release
  682. */
  683. if ((mbhc->current_plug !=
  684. MBHC_PLUG_TYPE_HEADSET) &&
  685. (mbhc->current_plug !=
  686. MBHC_PLUG_TYPE_ANC_HEADPHONE) &&
  687. !wcd_swch_level_remove(mbhc)) {
  688. pr_debug("%s: cable is %s headset\n",
  689. __func__,
  690. ((spl_hs_count ==
  691. WCD_MBHC_SPL_HS_CNT) ?
  692. "special ":""));
  693. goto report;
  694. }
  695. }
  696. wrk_complete = false;
  697. }
  698. }
  699. if (!wrk_complete) {
  700. /*
  701. * If plug_tye is headset, we might have already reported either
  702. * in detect_plug-type or in above while loop, no need to report
  703. * again
  704. */
  705. if ((plug_type == MBHC_PLUG_TYPE_HEADSET) ||
  706. (plug_type == MBHC_PLUG_TYPE_ANC_HEADPHONE)) {
  707. pr_debug("%s: plug_type:0x%x already reported\n",
  708. __func__, mbhc->current_plug);
  709. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_MODE, 0);
  710. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_EN, 0);
  711. goto enable_supply;
  712. }
  713. }
  714. if (plug_type == MBHC_PLUG_TYPE_HIGH_HPH) {
  715. if (wcd_is_special_headset(mbhc)) {
  716. pr_debug("%s: Special headset found %d\n",
  717. __func__, plug_type);
  718. plug_type = MBHC_PLUG_TYPE_HEADSET;
  719. } else {
  720. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_ISRC_EN, 1);
  721. }
  722. }
  723. report:
  724. if (wcd_swch_level_remove(mbhc)) {
  725. pr_debug("%s: Switch level is low\n", __func__);
  726. goto exit;
  727. }
  728. pr_debug("%s: Valid plug found, plug type %d wrk_cmpt %d btn_intr %d\n",
  729. __func__, plug_type, wrk_complete,
  730. mbhc->btn_press_intr);
  731. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_MODE, 0);
  732. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_EN, 0);
  733. WCD_MBHC_RSC_LOCK(mbhc);
  734. wcd_mbhc_find_plug_and_report(mbhc, plug_type);
  735. WCD_MBHC_RSC_UNLOCK(mbhc);
  736. enable_supply:
  737. /*
  738. * Set DETECTION_DONE bit for HEADSET and ANC_HEADPHONE,
  739. * so that btn press/release interrupt can be generated.
  740. * For other plug type, clear the bit.
  741. */
  742. if (plug_type == MBHC_PLUG_TYPE_HEADSET ||
  743. plug_type == MBHC_PLUG_TYPE_ANC_HEADPHONE)
  744. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_DETECTION_DONE, 1);
  745. else
  746. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_DETECTION_DONE, 0);
  747. if (mbhc->mbhc_cb->mbhc_micbias_control)
  748. wcd_mbhc_adc_update_fsm_source(mbhc, plug_type);
  749. exit:
  750. if (mbhc->mbhc_cb->mbhc_micbias_control &&
  751. !mbhc->micbias_enable)
  752. mbhc->mbhc_cb->mbhc_micbias_control(component, MIC_BIAS_2,
  753. MICB_DISABLE);
  754. /*
  755. * If plug type is corrected from special headset to headphone,
  756. * clear the micbias enable flag, set micbias back to 1.8V and
  757. * disable micbias.
  758. */
  759. if (plug_type == MBHC_PLUG_TYPE_HEADPHONE &&
  760. mbhc->micbias_enable) {
  761. if (mbhc->mbhc_cb->mbhc_micbias_control)
  762. mbhc->mbhc_cb->mbhc_micbias_control(
  763. component, MIC_BIAS_2,
  764. MICB_DISABLE);
  765. if (mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic)
  766. mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic(
  767. component,
  768. MIC_BIAS_2, false);
  769. if (mbhc->mbhc_cb->set_micbias_value) {
  770. mbhc->mbhc_cb->set_micbias_value(component);
  771. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MICB_CTRL, 0);
  772. }
  773. mbhc->micbias_enable = false;
  774. }
  775. if (mbhc->mbhc_cfg->detect_extn_cable &&
  776. ((plug_type == MBHC_PLUG_TYPE_HEADPHONE) ||
  777. (plug_type == MBHC_PLUG_TYPE_HEADSET)) &&
  778. !mbhc->hs_detect_work_stop) {
  779. WCD_MBHC_RSC_LOCK(mbhc);
  780. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_REM, true);
  781. WCD_MBHC_RSC_UNLOCK(mbhc);
  782. }
  783. /*
  784. * Enable ADC COMPLETE interrupt for HEADPHONE.
  785. * Btn release may happen after the correct work, ADC COMPLETE
  786. * interrupt needs to be captured to correct plug type.
  787. */
  788. if (plug_type == MBHC_PLUG_TYPE_HEADPHONE) {
  789. WCD_MBHC_RSC_LOCK(mbhc);
  790. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS,
  791. true);
  792. WCD_MBHC_RSC_UNLOCK(mbhc);
  793. }
  794. if (mbhc->mbhc_cb->hph_pull_down_ctrl)
  795. mbhc->mbhc_cb->hph_pull_down_ctrl(component, true);
  796. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  797. pr_debug("%s: leave\n", __func__);
  798. }
  799. static irqreturn_t wcd_mbhc_adc_hs_rem_irq(int irq, void *data)
  800. {
  801. struct wcd_mbhc *mbhc = data;
  802. unsigned long timeout;
  803. int adc_threshold, output_mv, retry = 0;
  804. bool hphpa_on = false;
  805. u8 moisture_status = 0;
  806. pr_debug("%s: enter\n", __func__);
  807. WCD_MBHC_RSC_LOCK(mbhc);
  808. timeout = jiffies +
  809. msecs_to_jiffies(WCD_FAKE_REMOVAL_MIN_PERIOD_MS);
  810. adc_threshold = ((WCD_MBHC_ADC_HS_THRESHOLD_MV *
  811. wcd_mbhc_get_micbias(mbhc)) /
  812. WCD_MBHC_ADC_MICBIAS_MV);
  813. do {
  814. retry++;
  815. /*
  816. * read output_mv every 10ms to look for
  817. * any change in IN2_P
  818. */
  819. usleep_range(10000, 10100);
  820. output_mv = wcd_measure_adc_once(mbhc, MUX_CTL_IN2P);
  821. pr_debug("%s: Check for fake removal: output_mv %d\n",
  822. __func__, output_mv);
  823. if ((output_mv <= adc_threshold) &&
  824. retry > FAKE_REM_RETRY_ATTEMPTS) {
  825. pr_debug("%s: headset is NOT actually removed\n",
  826. __func__);
  827. goto exit;
  828. }
  829. } while (!time_after(jiffies, timeout));
  830. if (wcd_swch_level_remove(mbhc)) {
  831. pr_debug("%s: Switch level is low ", __func__);
  832. goto exit;
  833. }
  834. if (mbhc->mbhc_cfg->moisture_en) {
  835. if (mbhc->mbhc_cb->hph_pa_on_status)
  836. if (mbhc->mbhc_cb->hph_pa_on_status(mbhc->component)) {
  837. hphpa_on = true;
  838. WCD_MBHC_REG_UPDATE_BITS(
  839. WCD_MBHC_HPHL_PA_EN, 0);
  840. WCD_MBHC_REG_UPDATE_BITS(
  841. WCD_MBHC_HPH_PA_EN, 0);
  842. }
  843. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHR_GND, 1);
  844. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHL_GND, 1);
  845. /* wait for 50ms to get moisture status */
  846. usleep_range(50000, 50100);
  847. WCD_MBHC_REG_READ(WCD_MBHC_MOISTURE_STATUS, moisture_status);
  848. }
  849. if (mbhc->mbhc_cfg->moisture_en && !moisture_status) {
  850. pr_debug("%s: moisture present in jack\n", __func__);
  851. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 0);
  852. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MECH_DETECTION_TYPE, 1);
  853. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 1);
  854. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  855. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  856. mbhc->btn_press_intr = false;
  857. mbhc->is_btn_press = false;
  858. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADSET)
  859. wcd_mbhc_report_plug(mbhc, 0, SND_JACK_HEADSET);
  860. else if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADPHONE)
  861. wcd_mbhc_report_plug(mbhc, 0, SND_JACK_HEADPHONE);
  862. else if (mbhc->current_plug == MBHC_PLUG_TYPE_GND_MIC_SWAP)
  863. wcd_mbhc_report_plug(mbhc, 0, SND_JACK_UNSUPPORTED);
  864. else if (mbhc->current_plug == MBHC_PLUG_TYPE_HIGH_HPH)
  865. wcd_mbhc_report_plug(mbhc, 0, SND_JACK_LINEOUT);
  866. } else {
  867. /*
  868. * ADC COMPLETE and ELEC_REM interrupts are both enabled for
  869. * HEADPHONE, need to reject the ADC COMPLETE interrupt which
  870. * follows ELEC_REM one when HEADPHONE is removed.
  871. */
  872. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADPHONE)
  873. mbhc->extn_cable_hph_rem = true;
  874. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_DETECTION_DONE, 0);
  875. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_MODE, 0);
  876. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ADC_EN, 0);
  877. wcd_mbhc_elec_hs_report_unplug(mbhc);
  878. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  879. if (hphpa_on) {
  880. hphpa_on = false;
  881. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHL_PA_EN, 1);
  882. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPH_PA_EN, 1);
  883. }
  884. }
  885. exit:
  886. WCD_MBHC_RSC_UNLOCK(mbhc);
  887. pr_debug("%s: leave\n", __func__);
  888. return IRQ_HANDLED;
  889. }
  890. static irqreturn_t wcd_mbhc_adc_hs_ins_irq(int irq, void *data)
  891. {
  892. struct wcd_mbhc *mbhc = data;
  893. u8 clamp_state = 0;
  894. u8 clamp_retry = WCD_MBHC_FAKE_INS_RETRY;
  895. pr_debug("%s: enter\n", __func__);
  896. /*
  897. * ADC COMPLETE and ELEC_REM interrupts are both enabled for HEADPHONE,
  898. * need to reject the ADC COMPLETE interrupt which follows ELEC_REM one
  899. * when HEADPHONE is removed.
  900. */
  901. if (mbhc->extn_cable_hph_rem == true) {
  902. mbhc->extn_cable_hph_rem = false;
  903. pr_debug("%s: leave\n", __func__);
  904. return IRQ_HANDLED;
  905. }
  906. do {
  907. WCD_MBHC_REG_READ(WCD_MBHC_IN2P_CLAMP_STATE, clamp_state);
  908. if (clamp_state) {
  909. pr_debug("%s: fake insertion irq, leave\n", __func__);
  910. return IRQ_HANDLED;
  911. }
  912. /*
  913. * check clamp for 120ms but at 30ms chunks to leave
  914. * room for other interrupts to be processed
  915. */
  916. usleep_range(30000, 30100);
  917. } while (--clamp_retry);
  918. WCD_MBHC_RSC_LOCK(mbhc);
  919. /*
  920. * If current plug is headphone then there is no chance to
  921. * get ADC complete interrupt, so connected cable should be
  922. * headset not headphone.
  923. */
  924. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADPHONE) {
  925. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS, false);
  926. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_DETECTION_DONE, 1);
  927. wcd_mbhc_find_plug_and_report(mbhc, MBHC_PLUG_TYPE_HEADSET);
  928. WCD_MBHC_RSC_UNLOCK(mbhc);
  929. return IRQ_HANDLED;
  930. }
  931. if (!mbhc->mbhc_cfg->detect_extn_cable) {
  932. pr_debug("%s: Returning as Extension cable feature not enabled\n",
  933. __func__);
  934. WCD_MBHC_RSC_UNLOCK(mbhc);
  935. return IRQ_HANDLED;
  936. }
  937. pr_debug("%s: Disable electrical headset insertion interrupt\n",
  938. __func__);
  939. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS, false);
  940. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_SCHMT_ISRC, 0);
  941. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_ISRC_EN, 0);
  942. mbhc->is_extn_cable = true;
  943. mbhc->btn_press_intr = false;
  944. wcd_mbhc_adc_detect_plug_type(mbhc);
  945. WCD_MBHC_RSC_UNLOCK(mbhc);
  946. pr_debug("%s: leave\n", __func__);
  947. return IRQ_HANDLED;
  948. }
  949. static struct wcd_mbhc_fn mbhc_fn = {
  950. .wcd_mbhc_hs_ins_irq = wcd_mbhc_adc_hs_ins_irq,
  951. .wcd_mbhc_hs_rem_irq = wcd_mbhc_adc_hs_rem_irq,
  952. .wcd_mbhc_detect_plug_type = wcd_mbhc_adc_detect_plug_type,
  953. .wcd_mbhc_detect_anc_plug_type = wcd_mbhc_adc_detect_anc_plug_type,
  954. .wcd_cancel_hs_detect_plug = wcd_cancel_hs_detect_plug,
  955. };
  956. /* Function: wcd_mbhc_adc_init
  957. * @mbhc: MBHC function pointer
  958. * Description: Initialize MBHC ADC related function pointers to MBHC structure
  959. */
  960. void wcd_mbhc_adc_init(struct wcd_mbhc *mbhc)
  961. {
  962. if (!mbhc) {
  963. pr_err("%s: mbhc is NULL\n", __func__);
  964. return;
  965. }
  966. mbhc->mbhc_fn = &mbhc_fn;
  967. INIT_WORK(&mbhc->correct_plug_swch, wcd_correct_swch_plug);
  968. }
  969. EXPORT_SYMBOL(wcd_mbhc_adc_init);