wcd-mbhc-v2.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2015-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 <linux/soc/qcom/fsa4480-i2c.h>
  21. #include <linux/usb/typec.h>
  22. #include <sound/soc.h>
  23. #include <sound/jack.h>
  24. #include <asoc/msm-cdc-pinctrl.h>
  25. #include <asoc/wcdcal-hwdep.h>
  26. #include "wcd-mbhc-legacy.h"
  27. #include "wcd-mbhc-adc.h"
  28. #include <asoc/wcd-mbhc-v2-api.h>
  29. void wcd_mbhc_jack_report(struct wcd_mbhc *mbhc,
  30. struct snd_soc_jack *jack, int status, int mask)
  31. {
  32. snd_soc_jack_report(jack, status, mask);
  33. }
  34. EXPORT_SYMBOL(wcd_mbhc_jack_report);
  35. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  36. static void __hphocp_off_report(struct wcd_mbhc *mbhc, u32 jack_status,
  37. int irq)
  38. {
  39. struct snd_soc_component *component = mbhc->component;
  40. dev_dbg(component->dev, "%s: clear ocp status %x\n",
  41. __func__, jack_status);
  42. if (mbhc->hph_status & jack_status) {
  43. mbhc->hph_status &= ~jack_status;
  44. wcd_mbhc_jack_report(mbhc, &mbhc->headset_jack,
  45. mbhc->hph_status, WCD_MBHC_JACK_MASK);
  46. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 0);
  47. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 1);
  48. /*
  49. * reset retry counter as PA is turned off signifying
  50. * start of new OCP detection session
  51. */
  52. if (mbhc->intr_ids->hph_left_ocp)
  53. mbhc->hphlocp_cnt = 0;
  54. else
  55. mbhc->hphrocp_cnt = 0;
  56. mbhc->mbhc_cb->irq_control(component, irq, true);
  57. }
  58. }
  59. static void hphrocp_off_report(struct wcd_mbhc *mbhc, u32 jack_status)
  60. {
  61. __hphocp_off_report(mbhc, SND_JACK_OC_HPHR,
  62. mbhc->intr_ids->hph_right_ocp);
  63. }
  64. static void hphlocp_off_report(struct wcd_mbhc *mbhc, u32 jack_status)
  65. {
  66. __hphocp_off_report(mbhc, SND_JACK_OC_HPHL,
  67. mbhc->intr_ids->hph_left_ocp);
  68. }
  69. #endif /* CONFIG_AUDIO_QGKI */
  70. static void wcd_program_hs_vref(struct wcd_mbhc *mbhc)
  71. {
  72. struct wcd_mbhc_plug_type_cfg *plug_type_cfg;
  73. struct snd_soc_component *component = mbhc->component;
  74. u32 reg_val;
  75. plug_type_cfg = WCD_MBHC_CAL_PLUG_TYPE_PTR(mbhc->mbhc_cfg->calibration);
  76. reg_val = ((plug_type_cfg->v_hs_max - HS_VREF_MIN_VAL) / 100);
  77. dev_dbg(component->dev, "%s: reg_val = %x\n", __func__, reg_val);
  78. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HS_VREF, reg_val);
  79. }
  80. static void wcd_program_btn_threshold(const struct wcd_mbhc *mbhc, bool micbias)
  81. {
  82. struct wcd_mbhc_btn_detect_cfg *btn_det;
  83. struct snd_soc_component *component = mbhc->component;
  84. struct snd_soc_card *card = component->card;
  85. s16 *btn_low, *btn_high;
  86. if (mbhc->mbhc_cfg->calibration == NULL) {
  87. dev_err(card->dev, "%s: calibration data is NULL\n", __func__);
  88. return;
  89. }
  90. btn_det = WCD_MBHC_CAL_BTN_DET_PTR(mbhc->mbhc_cfg->calibration);
  91. btn_low = btn_det->_v_btn_low;
  92. btn_high = ((void *)&btn_det->_v_btn_low) +
  93. (sizeof(btn_det->_v_btn_low[0]) * btn_det->num_btn);
  94. mbhc->mbhc_cb->set_btn_thr(component, btn_low, btn_high,
  95. btn_det->num_btn, micbias);
  96. }
  97. void wcd_enable_curr_micbias(const struct wcd_mbhc *mbhc,
  98. const enum wcd_mbhc_cs_mb_en_flag cs_mb_en)
  99. {
  100. /*
  101. * Some codecs handle micbias/pullup enablement in codec
  102. * drivers itself and micbias is not needed for regular
  103. * plug type detection. So if micbias_control callback function
  104. * is defined, just return.
  105. */
  106. if (mbhc->mbhc_cb->mbhc_micbias_control)
  107. return;
  108. pr_debug("%s: enter, cs_mb_en: %d\n", __func__, cs_mb_en);
  109. switch (cs_mb_en) {
  110. case WCD_MBHC_EN_CS:
  111. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MICB_CTRL, 0);
  112. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 3);
  113. /* Program Button threshold registers as per CS */
  114. wcd_program_btn_threshold(mbhc, false);
  115. break;
  116. case WCD_MBHC_EN_MB:
  117. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  118. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 1);
  119. /* Disable PULL_UP_EN & enable MICBIAS */
  120. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MICB_CTRL, 2);
  121. /* Program Button threshold registers as per MICBIAS */
  122. wcd_program_btn_threshold(mbhc, true);
  123. break;
  124. case WCD_MBHC_EN_PULLUP:
  125. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 3);
  126. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 1);
  127. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MICB_CTRL, 1);
  128. /* Program Button threshold registers as per MICBIAS */
  129. wcd_program_btn_threshold(mbhc, true);
  130. break;
  131. case WCD_MBHC_EN_NONE:
  132. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  133. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 1);
  134. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MICB_CTRL, 0);
  135. break;
  136. default:
  137. pr_debug("%s: Invalid parameter", __func__);
  138. break;
  139. }
  140. pr_debug("%s: exit\n", __func__);
  141. }
  142. EXPORT_SYMBOL(wcd_enable_curr_micbias);
  143. static const char *wcd_mbhc_get_event_string(int event)
  144. {
  145. switch (event) {
  146. case WCD_EVENT_PRE_MICBIAS_2_OFF:
  147. return WCD_MBHC_STRINGIFY(WCD_EVENT_PRE_MICBIAS_2_OFF);
  148. case WCD_EVENT_POST_MICBIAS_2_OFF:
  149. return WCD_MBHC_STRINGIFY(WCD_EVENT_POST_MICBIAS_2_OFF);
  150. case WCD_EVENT_PRE_MICBIAS_2_ON:
  151. return WCD_MBHC_STRINGIFY(WCD_EVENT_PRE_MICBIAS_2_ON);
  152. case WCD_EVENT_POST_MICBIAS_2_ON:
  153. return WCD_MBHC_STRINGIFY(WCD_EVENT_POST_MICBIAS_2_ON);
  154. case WCD_EVENT_PRE_HPHL_PA_ON:
  155. return WCD_MBHC_STRINGIFY(WCD_EVENT_PRE_HPHL_PA_ON);
  156. case WCD_EVENT_POST_HPHL_PA_OFF:
  157. return WCD_MBHC_STRINGIFY(WCD_EVENT_POST_HPHL_PA_OFF);
  158. case WCD_EVENT_PRE_HPHR_PA_ON:
  159. return WCD_MBHC_STRINGIFY(WCD_EVENT_PRE_HPHR_PA_ON);
  160. case WCD_EVENT_POST_HPHR_PA_OFF:
  161. return WCD_MBHC_STRINGIFY(WCD_EVENT_POST_HPHR_PA_OFF);
  162. case WCD_EVENT_PRE_HPHR_PA_OFF:
  163. return WCD_MBHC_STRINGIFY(WCD_EVENT_PRE_HPHR_PA_OFF);
  164. case WCD_EVENT_PRE_HPHL_PA_OFF:
  165. return WCD_MBHC_STRINGIFY(WCD_EVENT_PRE_HPHL_PA_OFF);
  166. case WCD_EVENT_POST_DAPM_MICBIAS_2_ON:
  167. return WCD_MBHC_STRINGIFY(WCD_EVENT_POST_DAPM_MICBIAS_2_ON);
  168. case WCD_EVENT_PRE_DAPM_MICBIAS_2_ON:
  169. return WCD_MBHC_STRINGIFY(WCD_EVENT_PRE_DAPM_MICBIAS_2_ON);
  170. case WCD_EVENT_POST_DAPM_MICBIAS_2_OFF:
  171. return WCD_MBHC_STRINGIFY(WCD_EVENT_POST_DAPM_MICBIAS_2_OFF);
  172. case WCD_EVENT_PRE_DAPM_MICBIAS_2_OFF:
  173. return WCD_MBHC_STRINGIFY(WCD_EVENT_PRE_DAPM_MICBIAS_2_OFF);
  174. case WCD_EVENT_OCP_OFF:
  175. return WCD_MBHC_STRINGIFY(WCD_EVENT_OCP_OFF);
  176. case WCD_EVENT_OCP_ON:
  177. return WCD_MBHC_STRINGIFY(WCD_EVENT_OCP_ON);
  178. case WCD_EVENT_INVALID:
  179. default:
  180. return WCD_MBHC_STRINGIFY(WCD_EVENT_INVALID);
  181. }
  182. }
  183. static int wcd_event_notify(struct notifier_block *self, unsigned long val,
  184. void *data)
  185. {
  186. struct wcd_mbhc *mbhc = (struct wcd_mbhc *)data;
  187. enum wcd_notify_event event = (enum wcd_notify_event)val;
  188. struct snd_soc_component *component = mbhc->component;
  189. bool micbias2 = false;
  190. bool micbias1 = false;
  191. u8 fsm_en = 0;
  192. pr_debug("%s: event %s (%d)\n", __func__,
  193. wcd_mbhc_get_event_string(event), event);
  194. if (mbhc->mbhc_cb->micbias_enable_status) {
  195. micbias2 = mbhc->mbhc_cb->micbias_enable_status(mbhc,
  196. MIC_BIAS_2);
  197. micbias1 = mbhc->mbhc_cb->micbias_enable_status(mbhc,
  198. MIC_BIAS_1);
  199. }
  200. switch (event) {
  201. /* MICBIAS usage change */
  202. case WCD_EVENT_POST_DAPM_MICBIAS_2_ON:
  203. mbhc->is_hs_recording = true;
  204. pr_debug("%s: is_capture: %d\n", __func__,
  205. mbhc->is_hs_recording);
  206. break;
  207. case WCD_EVENT_POST_MICBIAS_2_ON:
  208. if (!mbhc->micbias_enable)
  209. goto out_micb_en;
  210. if (mbhc->mbhc_cb->mbhc_common_micb_ctrl) {
  211. mbhc->mbhc_cb->mbhc_common_micb_ctrl(component,
  212. MBHC_COMMON_MICB_PRECHARGE,
  213. true);
  214. mbhc->mbhc_cb->mbhc_common_micb_ctrl(component,
  215. MBHC_COMMON_MICB_SET_VAL,
  216. true);
  217. /*
  218. * Special headset needs MICBIAS as 2.7V so wait for
  219. * 50 msec for the MICBIAS to reach 2.7 volts.
  220. */
  221. msleep(50);
  222. }
  223. if (mbhc->mbhc_cb->set_auto_zeroing)
  224. mbhc->mbhc_cb->set_auto_zeroing(component, true);
  225. if (mbhc->mbhc_cb->mbhc_common_micb_ctrl)
  226. mbhc->mbhc_cb->mbhc_common_micb_ctrl(component,
  227. MBHC_COMMON_MICB_PRECHARGE,
  228. false);
  229. out_micb_en:
  230. /* Disable current source if micbias enabled */
  231. if (mbhc->mbhc_cb->mbhc_micbias_control) {
  232. WCD_MBHC_REG_READ(WCD_MBHC_FSM_EN, fsm_en);
  233. if (fsm_en)
  234. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL,
  235. 0);
  236. } else {
  237. mbhc->is_hs_recording = true;
  238. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_MB);
  239. }
  240. /* configure cap settings properly when micbias is enabled */
  241. if (mbhc->mbhc_cb->set_cap_mode)
  242. mbhc->mbhc_cb->set_cap_mode(component, micbias1, true);
  243. break;
  244. case WCD_EVENT_PRE_MICBIAS_2_OFF:
  245. /*
  246. * Before MICBIAS_2 is turned off, if FSM is enabled,
  247. * make sure current source is enabled so as to detect
  248. * button press/release events
  249. */
  250. if (mbhc->mbhc_cb->mbhc_micbias_control &&
  251. !mbhc->micbias_enable) {
  252. WCD_MBHC_REG_READ(WCD_MBHC_FSM_EN, fsm_en);
  253. if (fsm_en)
  254. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL,
  255. 3);
  256. }
  257. break;
  258. /* MICBIAS usage change */
  259. case WCD_EVENT_POST_DAPM_MICBIAS_2_OFF:
  260. mbhc->is_hs_recording = false;
  261. pr_debug("%s: is_capture: %d\n", __func__,
  262. mbhc->is_hs_recording);
  263. break;
  264. case WCD_EVENT_POST_MICBIAS_2_OFF:
  265. if (!mbhc->mbhc_cb->mbhc_micbias_control)
  266. mbhc->is_hs_recording = false;
  267. if (mbhc->micbias_enable) {
  268. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_MB);
  269. break;
  270. }
  271. if (mbhc->mbhc_cb->set_auto_zeroing)
  272. mbhc->mbhc_cb->set_auto_zeroing(component, false);
  273. if (mbhc->mbhc_cb->set_micbias_value && !mbhc->micbias_enable)
  274. mbhc->mbhc_cb->set_micbias_value(component);
  275. /* Enable PULL UP if PA's are enabled */
  276. if ((test_bit(WCD_MBHC_EVENT_PA_HPHL, &mbhc->event_state)) ||
  277. (test_bit(WCD_MBHC_EVENT_PA_HPHR,
  278. &mbhc->event_state)))
  279. /* enable pullup and cs, disable mb */
  280. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_PULLUP);
  281. else
  282. /* enable current source and disable mb, pullup*/
  283. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_CS);
  284. /* configure cap settings properly when micbias is disabled */
  285. if (mbhc->mbhc_cb->set_cap_mode)
  286. mbhc->mbhc_cb->set_cap_mode(component, micbias1, false);
  287. break;
  288. case WCD_EVENT_PRE_HPHL_PA_OFF:
  289. mutex_lock(&mbhc->hphl_pa_lock);
  290. break;
  291. case WCD_EVENT_POST_HPHL_PA_OFF:
  292. clear_bit(WCD_MBHC_HPHL_PA_OFF_ACK, &mbhc->hph_pa_dac_state);
  293. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  294. if (mbhc->hph_status & SND_JACK_OC_HPHL)
  295. hphlocp_off_report(mbhc, SND_JACK_OC_HPHL);
  296. #endif /* CONFIG_AUDIO_QGKI */
  297. clear_bit(WCD_MBHC_EVENT_PA_HPHL, &mbhc->event_state);
  298. /* check if micbias is enabled */
  299. if (micbias2)
  300. /* Disable cs, pullup & enable micbias */
  301. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_MB);
  302. else
  303. /* Disable micbias, pullup & enable cs */
  304. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_CS);
  305. mutex_unlock(&mbhc->hphl_pa_lock);
  306. clear_bit(WCD_MBHC_ANC0_OFF_ACK, &mbhc->hph_anc_state);
  307. break;
  308. case WCD_EVENT_PRE_HPHR_PA_OFF:
  309. mutex_lock(&mbhc->hphr_pa_lock);
  310. break;
  311. case WCD_EVENT_POST_HPHR_PA_OFF:
  312. clear_bit(WCD_MBHC_HPHR_PA_OFF_ACK, &mbhc->hph_pa_dac_state);
  313. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  314. if (mbhc->hph_status & SND_JACK_OC_HPHR)
  315. hphrocp_off_report(mbhc, SND_JACK_OC_HPHR);
  316. #endif /* CONFIG_AUDIO_QGKI */
  317. clear_bit(WCD_MBHC_EVENT_PA_HPHR, &mbhc->event_state);
  318. /* check if micbias is enabled */
  319. if (micbias2)
  320. /* Disable cs, pullup & enable micbias */
  321. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_MB);
  322. else
  323. /* Disable micbias, pullup & enable cs */
  324. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_CS);
  325. mutex_unlock(&mbhc->hphr_pa_lock);
  326. clear_bit(WCD_MBHC_ANC1_OFF_ACK, &mbhc->hph_anc_state);
  327. break;
  328. case WCD_EVENT_PRE_HPHL_PA_ON:
  329. set_bit(WCD_MBHC_EVENT_PA_HPHL, &mbhc->event_state);
  330. /* check if micbias is enabled */
  331. if (micbias2)
  332. /* Disable cs, pullup & enable micbias */
  333. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_MB);
  334. else
  335. /* Disable micbias, enable pullup & cs */
  336. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_PULLUP);
  337. break;
  338. case WCD_EVENT_PRE_HPHR_PA_ON:
  339. set_bit(WCD_MBHC_EVENT_PA_HPHR, &mbhc->event_state);
  340. /* check if micbias is enabled */
  341. if (micbias2)
  342. /* Disable cs, pullup & enable micbias */
  343. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_MB);
  344. else
  345. /* Disable micbias, enable pullup & cs */
  346. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_PULLUP);
  347. break;
  348. case WCD_EVENT_OCP_OFF:
  349. mbhc->mbhc_cb->irq_control(mbhc->component,
  350. mbhc->intr_ids->hph_left_ocp,
  351. false);
  352. break;
  353. case WCD_EVENT_OCP_ON:
  354. mbhc->mbhc_cb->irq_control(mbhc->component,
  355. mbhc->intr_ids->hph_left_ocp,
  356. true);
  357. break;
  358. default:
  359. break;
  360. }
  361. return 0;
  362. }
  363. int wcd_cancel_btn_work(struct wcd_mbhc *mbhc)
  364. {
  365. int r;
  366. r = cancel_delayed_work_sync(&mbhc->mbhc_btn_dwork);
  367. /*
  368. * if scheduled mbhc.mbhc_btn_dwork is canceled from here,
  369. * we have to unlock from here instead btn_work
  370. */
  371. if (r)
  372. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  373. return r;
  374. }
  375. EXPORT_SYMBOL(wcd_cancel_btn_work);
  376. bool wcd_swch_level_remove(struct wcd_mbhc *mbhc)
  377. {
  378. u16 result2 = 0;
  379. WCD_MBHC_REG_READ(WCD_MBHC_SWCH_LEVEL_REMOVE, result2);
  380. return (result2) ? true : false;
  381. }
  382. EXPORT_SYMBOL(wcd_swch_level_remove);
  383. static void wcd_mbhc_clr_and_turnon_hph_padac(struct wcd_mbhc *mbhc)
  384. {
  385. bool pa_turned_on = false;
  386. u8 wg_time = 0;
  387. WCD_MBHC_REG_READ(WCD_MBHC_HPH_CNP_WG_TIME, wg_time);
  388. wg_time += 1;
  389. mutex_lock(&mbhc->hphr_pa_lock);
  390. if (test_and_clear_bit(WCD_MBHC_HPHR_PA_OFF_ACK,
  391. &mbhc->hph_pa_dac_state)) {
  392. pr_debug("%s: HPHR clear flag and enable PA\n", __func__);
  393. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHR_PA_EN, 1);
  394. pa_turned_on = true;
  395. }
  396. mutex_unlock(&mbhc->hphr_pa_lock);
  397. mutex_lock(&mbhc->hphl_pa_lock);
  398. if (test_and_clear_bit(WCD_MBHC_HPHL_PA_OFF_ACK,
  399. &mbhc->hph_pa_dac_state)) {
  400. pr_debug("%s: HPHL clear flag and enable PA\n", __func__);
  401. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHL_PA_EN, 1);
  402. pa_turned_on = true;
  403. }
  404. mutex_unlock(&mbhc->hphl_pa_lock);
  405. if (pa_turned_on) {
  406. pr_debug("%s: PA was turned on by MBHC and not by DAPM\n",
  407. __func__);
  408. usleep_range(wg_time * 1000, wg_time * 1000 + 50);
  409. }
  410. if (test_and_clear_bit(WCD_MBHC_ANC0_OFF_ACK,
  411. &mbhc->hph_anc_state)) {
  412. usleep_range(20000, 20100);
  413. pr_debug("%s: HPHL ANC clear flag and enable ANC_EN\n",
  414. __func__);
  415. if (mbhc->mbhc_cb->update_anc_state)
  416. mbhc->mbhc_cb->update_anc_state(mbhc->component,
  417. true, 0);
  418. }
  419. if (test_and_clear_bit(WCD_MBHC_ANC1_OFF_ACK,
  420. &mbhc->hph_anc_state)) {
  421. usleep_range(20000, 20100);
  422. pr_debug("%s: HPHR ANC clear flag and enable ANC_EN\n",
  423. __func__);
  424. if (mbhc->mbhc_cb->update_anc_state)
  425. mbhc->mbhc_cb->update_anc_state(mbhc->component,
  426. true, 1);
  427. }
  428. }
  429. static bool wcd_mbhc_is_hph_pa_on(struct wcd_mbhc *mbhc)
  430. {
  431. bool hph_pa_on = false;
  432. WCD_MBHC_REG_READ(WCD_MBHC_HPH_PA_EN, hph_pa_on);
  433. return (hph_pa_on) ? true : false;
  434. }
  435. static void wcd_mbhc_set_and_turnoff_hph_padac(struct wcd_mbhc *mbhc)
  436. {
  437. u8 wg_time = 0;
  438. WCD_MBHC_REG_READ(WCD_MBHC_HPH_CNP_WG_TIME, wg_time);
  439. wg_time += 1;
  440. /* If headphone PA is on, check if userspace receives
  441. * removal event to sync-up PA's state
  442. */
  443. if (wcd_mbhc_is_hph_pa_on(mbhc)) {
  444. pr_debug("%s PA is on, setting PA_OFF_ACK\n", __func__);
  445. set_bit(WCD_MBHC_HPHL_PA_OFF_ACK, &mbhc->hph_pa_dac_state);
  446. set_bit(WCD_MBHC_HPHR_PA_OFF_ACK, &mbhc->hph_pa_dac_state);
  447. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHL_OCP_DET_EN, 0);
  448. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHR_OCP_DET_EN, 0);
  449. } else {
  450. pr_debug("%s PA is off\n", __func__);
  451. }
  452. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPH_PA_EN, 0);
  453. usleep_range(wg_time * 1000, wg_time * 1000 + 50);
  454. if (mbhc->mbhc_cb->is_anc_on && mbhc->mbhc_cb->is_anc_on(mbhc)) {
  455. usleep_range(20000, 20100);
  456. pr_debug("%s ANC is on, setting ANC_OFF_ACK\n", __func__);
  457. set_bit(WCD_MBHC_ANC0_OFF_ACK, &mbhc->hph_anc_state);
  458. set_bit(WCD_MBHC_ANC1_OFF_ACK, &mbhc->hph_anc_state);
  459. if (mbhc->mbhc_cb->update_anc_state) {
  460. mbhc->mbhc_cb->update_anc_state(mbhc->component,
  461. false, 0);
  462. mbhc->mbhc_cb->update_anc_state(mbhc->component,
  463. false, 1);
  464. } else {
  465. pr_debug("%s ANC is off\n", __func__);
  466. }
  467. }
  468. }
  469. int wcd_mbhc_get_impedance(struct wcd_mbhc *mbhc, uint32_t *zl,
  470. uint32_t *zr)
  471. {
  472. *zl = mbhc->zl;
  473. *zr = mbhc->zr;
  474. if (*zl && *zr)
  475. return 0;
  476. else
  477. return -EINVAL;
  478. }
  479. EXPORT_SYMBOL(wcd_mbhc_get_impedance);
  480. void wcd_mbhc_hs_elec_irq(struct wcd_mbhc *mbhc, int irq_type,
  481. bool enable)
  482. {
  483. int irq;
  484. WCD_MBHC_RSC_ASSERT_LOCKED(mbhc);
  485. if (irq_type == WCD_MBHC_ELEC_HS_INS)
  486. irq = mbhc->intr_ids->mbhc_hs_ins_intr;
  487. else if (irq_type == WCD_MBHC_ELEC_HS_REM)
  488. irq = mbhc->intr_ids->mbhc_hs_rem_intr;
  489. else {
  490. pr_debug("%s: irq_type: %d, enable: %d\n",
  491. __func__, irq_type, enable);
  492. return;
  493. }
  494. pr_debug("%s: irq: %d, enable: %d, intr_status:%lu\n",
  495. __func__, irq, enable, mbhc->intr_status);
  496. if ((test_bit(irq_type, &mbhc->intr_status)) != enable) {
  497. mbhc->mbhc_cb->irq_control(mbhc->component, irq, enable);
  498. if (enable)
  499. set_bit(irq_type, &mbhc->intr_status);
  500. else
  501. clear_bit(irq_type, &mbhc->intr_status);
  502. }
  503. }
  504. EXPORT_SYMBOL(wcd_mbhc_hs_elec_irq);
  505. void wcd_mbhc_report_plug(struct wcd_mbhc *mbhc, int insertion,
  506. enum snd_jack_types jack_type)
  507. {
  508. struct snd_soc_component *component = mbhc->component;
  509. bool is_pa_on = false;
  510. u8 fsm_en = 0;
  511. WCD_MBHC_RSC_ASSERT_LOCKED(mbhc);
  512. pr_debug("%s: enter insertion %d hph_status %x\n",
  513. __func__, insertion, mbhc->hph_status);
  514. if (!insertion) {
  515. /* Report removal */
  516. mbhc->hph_status &= ~jack_type;
  517. /*
  518. * cancel possibly scheduled btn work and
  519. * report release if we reported button press
  520. */
  521. if (wcd_cancel_btn_work(mbhc)) {
  522. pr_debug("%s: button press is canceled\n", __func__);
  523. } else if (mbhc->buttons_pressed) {
  524. pr_debug("%s: release of button press%d\n",
  525. __func__, jack_type);
  526. wcd_mbhc_jack_report(mbhc, &mbhc->button_jack, 0,
  527. mbhc->buttons_pressed);
  528. mbhc->buttons_pressed &=
  529. ~WCD_MBHC_JACK_BUTTON_MASK;
  530. }
  531. if (mbhc->micbias_enable) {
  532. if (mbhc->mbhc_cb->mbhc_micbias_control)
  533. mbhc->mbhc_cb->mbhc_micbias_control(
  534. component, MIC_BIAS_2,
  535. MICB_DISABLE);
  536. if (mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic)
  537. mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic(
  538. component,
  539. MIC_BIAS_2, false);
  540. if (mbhc->mbhc_cb->set_micbias_value) {
  541. mbhc->mbhc_cb->set_micbias_value(component);
  542. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MICB_CTRL, 0);
  543. }
  544. mbhc->micbias_enable = false;
  545. }
  546. mbhc->hph_type = WCD_MBHC_HPH_NONE;
  547. mbhc->zl = mbhc->zr = 0;
  548. pr_debug("%s: Reporting removal %d(%x)\n", __func__,
  549. jack_type, mbhc->hph_status);
  550. wcd_mbhc_jack_report(mbhc, &mbhc->headset_jack,
  551. mbhc->hph_status, WCD_MBHC_JACK_MASK);
  552. wcd_mbhc_set_and_turnoff_hph_padac(mbhc);
  553. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  554. hphrocp_off_report(mbhc, SND_JACK_OC_HPHR);
  555. hphlocp_off_report(mbhc, SND_JACK_OC_HPHL);
  556. #endif /* CONFIG_AUDIO_QGKI */
  557. mbhc->current_plug = MBHC_PLUG_TYPE_NONE;
  558. mbhc->force_linein = false;
  559. } else {
  560. /*
  561. * Report removal of current jack type.
  562. * Headphone to headset shouldn't report headphone
  563. * removal.
  564. */
  565. if (mbhc->mbhc_cfg->detect_extn_cable &&
  566. (mbhc->current_plug == MBHC_PLUG_TYPE_HIGH_HPH ||
  567. jack_type == SND_JACK_LINEOUT) &&
  568. (mbhc->hph_status && mbhc->hph_status != jack_type)) {
  569. if (mbhc->micbias_enable &&
  570. mbhc->hph_status == SND_JACK_HEADSET) {
  571. if (mbhc->mbhc_cb->mbhc_micbias_control)
  572. mbhc->mbhc_cb->mbhc_micbias_control(
  573. component, MIC_BIAS_2,
  574. MICB_DISABLE);
  575. if (mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic)
  576. mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic(
  577. component,
  578. MIC_BIAS_2, false);
  579. if (mbhc->mbhc_cb->set_micbias_value) {
  580. mbhc->mbhc_cb->set_micbias_value(
  581. component);
  582. WCD_MBHC_REG_UPDATE_BITS(
  583. WCD_MBHC_MICB_CTRL, 0);
  584. }
  585. mbhc->micbias_enable = false;
  586. }
  587. mbhc->hph_type = WCD_MBHC_HPH_NONE;
  588. mbhc->zl = mbhc->zr = 0;
  589. pr_debug("%s: Reporting removal (%x)\n",
  590. __func__, mbhc->hph_status);
  591. wcd_mbhc_jack_report(mbhc, &mbhc->headset_jack,
  592. 0, WCD_MBHC_JACK_MASK);
  593. if (mbhc->hph_status == SND_JACK_LINEOUT) {
  594. pr_debug("%s: Enable micbias\n", __func__);
  595. /* Disable current source and enable micbias */
  596. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_MB);
  597. pr_debug("%s: set up elec removal detection\n",
  598. __func__);
  599. usleep_range(200, 210);
  600. wcd_mbhc_hs_elec_irq(mbhc,
  601. WCD_MBHC_ELEC_HS_REM,
  602. true);
  603. }
  604. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  605. mbhc->hph_status &= ~(SND_JACK_HEADSET |
  606. SND_JACK_LINEOUT |
  607. SND_JACK_ANC_HEADPHONE |
  608. SND_JACK_UNSUPPORTED);
  609. #else
  610. mbhc->hph_status &= ~(SND_JACK_HEADSET |
  611. SND_JACK_LINEOUT);
  612. #endif /* CONFIG_AUDIO_QGKI */
  613. }
  614. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADSET &&
  615. jack_type == SND_JACK_HEADPHONE)
  616. mbhc->hph_status &= ~SND_JACK_HEADSET;
  617. /* Report insertion */
  618. if (jack_type == SND_JACK_HEADPHONE)
  619. mbhc->current_plug = MBHC_PLUG_TYPE_HEADPHONE;
  620. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  621. else if (jack_type == SND_JACK_UNSUPPORTED)
  622. mbhc->current_plug = MBHC_PLUG_TYPE_GND_MIC_SWAP;
  623. #endif /* CONFIG_AUDIO_QGKI */
  624. else if (jack_type == SND_JACK_HEADSET) {
  625. mbhc->current_plug = MBHC_PLUG_TYPE_HEADSET;
  626. mbhc->jiffies_atreport = jiffies;
  627. } else if (jack_type == SND_JACK_LINEOUT)
  628. mbhc->current_plug = MBHC_PLUG_TYPE_HIGH_HPH;
  629. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  630. else if (jack_type == SND_JACK_ANC_HEADPHONE)
  631. mbhc->current_plug = MBHC_PLUG_TYPE_ANC_HEADPHONE;
  632. #endif /* CONFIG_AUDIO_QGKI */
  633. if (mbhc->mbhc_cb->hph_pa_on_status)
  634. is_pa_on = mbhc->mbhc_cb->hph_pa_on_status(component);
  635. if (mbhc->impedance_detect &&
  636. mbhc->mbhc_cb->compute_impedance &&
  637. (mbhc->mbhc_cfg->linein_th != 0) &&
  638. (!is_pa_on)) {
  639. /* Set MUX_CTL to AUTO for Z-det */
  640. WCD_MBHC_REG_READ(WCD_MBHC_FSM_EN, fsm_en);
  641. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  642. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MUX_CTL,
  643. MUX_CTL_AUTO);
  644. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 1);
  645. mbhc->mbhc_cb->compute_impedance(mbhc,
  646. &mbhc->zl, &mbhc->zr);
  647. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN,
  648. fsm_en);
  649. if ((mbhc->zl > mbhc->mbhc_cfg->linein_th) &&
  650. (mbhc->zr > mbhc->mbhc_cfg->linein_th) &&
  651. (jack_type == SND_JACK_HEADPHONE)) {
  652. jack_type = SND_JACK_LINEOUT;
  653. mbhc->force_linein = true;
  654. mbhc->current_plug = MBHC_PLUG_TYPE_HIGH_HPH;
  655. if (mbhc->hph_status) {
  656. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  657. mbhc->hph_status &= ~(SND_JACK_HEADSET |
  658. SND_JACK_LINEOUT |
  659. SND_JACK_UNSUPPORTED);
  660. #else
  661. mbhc->hph_status &= ~(SND_JACK_HEADSET |
  662. SND_JACK_LINEOUT);
  663. #endif /* CONFIG_AUDIO_QGKI */
  664. wcd_mbhc_jack_report(mbhc,
  665. &mbhc->headset_jack,
  666. mbhc->hph_status,
  667. WCD_MBHC_JACK_MASK);
  668. }
  669. pr_debug("%s: Marking jack type as SND_JACK_LINEOUT\n",
  670. __func__);
  671. }
  672. }
  673. /* Do not calculate impedance again for lineout
  674. * as during playback pa is on and impedance values
  675. * will not be correct resulting in lineout detected
  676. * as headphone.
  677. */
  678. if ((is_pa_on) && mbhc->force_linein == true) {
  679. jack_type = SND_JACK_LINEOUT;
  680. mbhc->current_plug = MBHC_PLUG_TYPE_HIGH_HPH;
  681. if (mbhc->hph_status) {
  682. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  683. mbhc->hph_status &= ~(SND_JACK_HEADSET |
  684. SND_JACK_LINEOUT |
  685. SND_JACK_UNSUPPORTED);
  686. #else
  687. mbhc->hph_status &= ~(SND_JACK_HEADSET |
  688. SND_JACK_LINEOUT);
  689. #endif /* CONFIG_AUDIO_QGKI */
  690. wcd_mbhc_jack_report(mbhc,
  691. &mbhc->headset_jack,
  692. mbhc->hph_status,
  693. WCD_MBHC_JACK_MASK);
  694. }
  695. }
  696. mbhc->hph_status |= jack_type;
  697. pr_debug("%s: Reporting insertion %d(%x)\n", __func__,
  698. jack_type, mbhc->hph_status);
  699. wcd_mbhc_jack_report(mbhc, &mbhc->headset_jack,
  700. (mbhc->hph_status | SND_JACK_MECHANICAL),
  701. WCD_MBHC_JACK_MASK);
  702. wcd_mbhc_clr_and_turnon_hph_padac(mbhc);
  703. }
  704. pr_debug("%s: leave hph_status %x\n", __func__, mbhc->hph_status);
  705. }
  706. EXPORT_SYMBOL(wcd_mbhc_report_plug);
  707. void wcd_mbhc_elec_hs_report_unplug(struct wcd_mbhc *mbhc)
  708. {
  709. /* cancel pending button press */
  710. if (wcd_cancel_btn_work(mbhc))
  711. pr_debug("%s: button press is canceled\n", __func__);
  712. /* cancel correct work function */
  713. if (mbhc->mbhc_fn->wcd_cancel_hs_detect_plug)
  714. mbhc->mbhc_fn->wcd_cancel_hs_detect_plug(mbhc,
  715. &mbhc->correct_plug_swch);
  716. else
  717. pr_info("%s: hs_detect_plug work not cancelled\n", __func__);
  718. pr_debug("%s: Report extension cable\n", __func__);
  719. wcd_mbhc_report_plug(mbhc, 1, SND_JACK_LINEOUT);
  720. /*
  721. * If PA is enabled HPHL schmitt trigger can
  722. * be unreliable, make sure to disable it
  723. */
  724. if (test_bit(WCD_MBHC_EVENT_PA_HPHL,
  725. &mbhc->event_state))
  726. wcd_mbhc_set_and_turnoff_hph_padac(mbhc);
  727. /*
  728. * Disable HPHL trigger and MIC Schmitt triggers.
  729. * Setup for insertion detection.
  730. */
  731. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_REM,
  732. false);
  733. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_NONE);
  734. /* Disable HW FSM */
  735. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  736. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_SCHMT_ISRC, 3);
  737. /* Set the detection type appropriately */
  738. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_DETECTION_TYPE, 1);
  739. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS,
  740. true);
  741. }
  742. EXPORT_SYMBOL(wcd_mbhc_elec_hs_report_unplug);
  743. void wcd_mbhc_find_plug_and_report(struct wcd_mbhc *mbhc,
  744. enum wcd_mbhc_plug_type plug_type)
  745. {
  746. bool anc_mic_found = false;
  747. enum snd_jack_types jack_type;
  748. if (mbhc->deinit_in_progress) {
  749. pr_info("%s: mbhc deinit in progess: ignore report\n", __func__);
  750. return;
  751. }
  752. pr_debug("%s: enter current_plug(%d) new_plug(%d)\n",
  753. __func__, mbhc->current_plug, plug_type);
  754. WCD_MBHC_RSC_ASSERT_LOCKED(mbhc);
  755. if (mbhc->current_plug == plug_type) {
  756. pr_debug("%s: cable already reported, exit\n", __func__);
  757. goto exit;
  758. }
  759. if (plug_type == MBHC_PLUG_TYPE_HEADPHONE) {
  760. /*
  761. * Nothing was reported previously
  762. * report a headphone or unsupported
  763. */
  764. wcd_mbhc_report_plug(mbhc, 1, SND_JACK_HEADPHONE);
  765. } else if (plug_type == MBHC_PLUG_TYPE_GND_MIC_SWAP) {
  766. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADPHONE)
  767. wcd_mbhc_report_plug(mbhc, 0, SND_JACK_HEADPHONE);
  768. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADSET)
  769. wcd_mbhc_report_plug(mbhc, 0, SND_JACK_HEADSET);
  770. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  771. wcd_mbhc_report_plug(mbhc, 1, SND_JACK_UNSUPPORTED);
  772. #endif /* CONFIG_AUDIO_QGKI */
  773. } else if (plug_type == MBHC_PLUG_TYPE_HEADSET) {
  774. if (mbhc->mbhc_cfg->enable_anc_mic_detect &&
  775. mbhc->mbhc_fn->wcd_mbhc_detect_anc_plug_type)
  776. anc_mic_found =
  777. mbhc->mbhc_fn->wcd_mbhc_detect_anc_plug_type(mbhc);
  778. jack_type = SND_JACK_HEADSET;
  779. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  780. if (anc_mic_found)
  781. jack_type = SND_JACK_ANC_HEADPHONE;
  782. #endif /* CONFIG_AUDIO_QGKI */
  783. /*
  784. * If Headphone was reported previously, this will
  785. * only report the mic line
  786. */
  787. wcd_mbhc_report_plug(mbhc, 1, jack_type);
  788. } else if (plug_type == MBHC_PLUG_TYPE_HIGH_HPH) {
  789. if (mbhc->mbhc_cfg->detect_extn_cable) {
  790. /* High impedance device found. Report as LINEOUT */
  791. wcd_mbhc_report_plug(mbhc, 1, SND_JACK_LINEOUT);
  792. pr_debug("%s: setup mic trigger for further detection\n",
  793. __func__);
  794. /* Disable HW FSM and current source */
  795. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  796. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  797. /* Setup for insertion detection */
  798. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_DETECTION_TYPE,
  799. 1);
  800. /*
  801. * Enable HPHL trigger and MIC Schmitt triggers
  802. * and request for elec insertion interrupts
  803. */
  804. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_SCHMT_ISRC,
  805. 3);
  806. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS,
  807. true);
  808. } else {
  809. wcd_mbhc_report_plug(mbhc, 1, SND_JACK_LINEOUT);
  810. }
  811. } else {
  812. WARN(1, "Unexpected current plug_type %d, plug_type %d\n",
  813. mbhc->current_plug, plug_type);
  814. }
  815. exit:
  816. pr_debug("%s: leave\n", __func__);
  817. }
  818. EXPORT_SYMBOL(wcd_mbhc_find_plug_and_report);
  819. static bool wcd_mbhc_moisture_detect(struct wcd_mbhc *mbhc, bool detection_type)
  820. {
  821. bool ret = false;
  822. if (!mbhc->mbhc_cfg->moisture_en &&
  823. !mbhc->mbhc_cfg->moisture_duty_cycle_en)
  824. return ret;
  825. if (!mbhc->mbhc_cb->mbhc_get_moisture_status ||
  826. !mbhc->mbhc_cb->mbhc_moisture_polling_ctrl ||
  827. !mbhc->mbhc_cb->mbhc_moisture_detect_en)
  828. return ret;
  829. if (mbhc->mbhc_cb->mbhc_get_moisture_status(mbhc)) {
  830. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 0);
  831. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_GND_DET_EN, 0);
  832. mbhc->mbhc_cb->mbhc_moisture_polling_ctrl(mbhc, true);
  833. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MECH_DETECTION_TYPE,
  834. detection_type);
  835. ret = true;
  836. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 1);
  837. if (mbhc->mbhc_cfg->gnd_det_en)
  838. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_GND_DET_EN, 1);
  839. } else {
  840. mbhc->mbhc_cb->mbhc_moisture_polling_ctrl(mbhc, false);
  841. mbhc->mbhc_cb->mbhc_moisture_detect_en(mbhc, false);
  842. }
  843. return ret;
  844. }
  845. static void wcd_mbhc_swch_irq_handler(struct wcd_mbhc *mbhc)
  846. {
  847. bool detection_type = 0;
  848. bool micbias1 = false;
  849. struct snd_soc_component *component = mbhc->component;
  850. enum snd_jack_types jack_type;
  851. dev_dbg(component->dev, "%s: enter\n", __func__);
  852. WCD_MBHC_RSC_LOCK(mbhc);
  853. mbhc->in_swch_irq_handler = true;
  854. /* cancel pending button press */
  855. if (wcd_cancel_btn_work(mbhc))
  856. pr_debug("%s: button press is canceled\n", __func__);
  857. WCD_MBHC_REG_READ(WCD_MBHC_MECH_DETECTION_TYPE, detection_type);
  858. /* Set the detection type appropriately */
  859. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MECH_DETECTION_TYPE,
  860. !detection_type);
  861. pr_debug("%s: mbhc->current_plug: %d detection_type: %d\n", __func__,
  862. mbhc->current_plug, detection_type);
  863. if (mbhc->mbhc_fn->wcd_cancel_hs_detect_plug)
  864. mbhc->mbhc_fn->wcd_cancel_hs_detect_plug(mbhc,
  865. &mbhc->correct_plug_swch);
  866. else
  867. pr_info("%s: hs_detect_plug work not cancelled\n", __func__);
  868. if (mbhc->mbhc_cb->micbias_enable_status)
  869. micbias1 = mbhc->mbhc_cb->micbias_enable_status(mbhc,
  870. MIC_BIAS_1);
  871. if ((mbhc->current_plug == MBHC_PLUG_TYPE_NONE) &&
  872. detection_type) {
  873. /* If moisture is present, then enable polling, disable
  874. * moisture detection and wait for interrupt
  875. */
  876. if (wcd_mbhc_moisture_detect(mbhc, detection_type))
  877. goto done;
  878. /* Make sure MASTER_BIAS_CTL is enabled */
  879. mbhc->mbhc_cb->mbhc_bias(component, true);
  880. if (mbhc->mbhc_cb->mbhc_common_micb_ctrl)
  881. mbhc->mbhc_cb->mbhc_common_micb_ctrl(component,
  882. MBHC_COMMON_MICB_TAIL_CURR, true);
  883. if (!mbhc->mbhc_cfg->hs_ext_micbias &&
  884. mbhc->mbhc_cb->micb_internal)
  885. /*
  886. * Enable Tx2 RBias if the headset
  887. * is using internal micbias
  888. */
  889. mbhc->mbhc_cb->micb_internal(component, 1, true);
  890. /* Remove micbias pulldown */
  891. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_PULLDOWN_CTRL, 0);
  892. /* Apply trim if needed on the device */
  893. if (mbhc->mbhc_cb->trim_btn_reg)
  894. mbhc->mbhc_cb->trim_btn_reg(component);
  895. /* Enable external voltage source to micbias if present */
  896. if (mbhc->mbhc_cb->enable_mb_source)
  897. mbhc->mbhc_cb->enable_mb_source(mbhc, true);
  898. mbhc->btn_press_intr = false;
  899. mbhc->is_btn_press = false;
  900. if (mbhc->mbhc_fn)
  901. mbhc->mbhc_fn->wcd_mbhc_detect_plug_type(mbhc);
  902. } else if ((mbhc->current_plug != MBHC_PLUG_TYPE_NONE)
  903. && !detection_type) {
  904. /* Disable external voltage source to micbias if present */
  905. if (mbhc->mbhc_cb->enable_mb_source)
  906. mbhc->mbhc_cb->enable_mb_source(mbhc, false);
  907. /* Disable HW FSM */
  908. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  909. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  910. if (mbhc->mbhc_cb->mbhc_common_micb_ctrl)
  911. mbhc->mbhc_cb->mbhc_common_micb_ctrl(component,
  912. MBHC_COMMON_MICB_TAIL_CURR, false);
  913. if (mbhc->mbhc_cb->set_cap_mode)
  914. mbhc->mbhc_cb->set_cap_mode(component, micbias1, false);
  915. mbhc->btn_press_intr = false;
  916. mbhc->is_btn_press = false;
  917. switch (mbhc->current_plug) {
  918. case MBHC_PLUG_TYPE_HEADPHONE:
  919. jack_type = SND_JACK_HEADPHONE;
  920. break;
  921. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  922. case MBHC_PLUG_TYPE_GND_MIC_SWAP:
  923. jack_type = SND_JACK_UNSUPPORTED;
  924. break;
  925. #endif /* CONFIG_AUDIO_QGKI */
  926. case MBHC_PLUG_TYPE_HEADSET:
  927. /* make sure to turn off Rbias */
  928. if (mbhc->mbhc_cb->micb_internal)
  929. mbhc->mbhc_cb->micb_internal(component,
  930. 1, false);
  931. /* Pulldown micbias */
  932. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_PULLDOWN_CTRL, 1);
  933. jack_type = SND_JACK_HEADSET;
  934. break;
  935. case MBHC_PLUG_TYPE_HIGH_HPH:
  936. if (mbhc->mbhc_detection_logic == WCD_DETECTION_ADC)
  937. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_ISRC_EN, 0);
  938. mbhc->is_extn_cable = false;
  939. jack_type = SND_JACK_LINEOUT;
  940. break;
  941. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  942. case MBHC_PLUG_TYPE_ANC_HEADPHONE:
  943. jack_type = SND_JACK_ANC_HEADPHONE;
  944. break;
  945. #endif /* CONFIG_AUDIO_QGKI */
  946. default:
  947. pr_info("%s: Invalid current plug: %d\n",
  948. __func__, mbhc->current_plug);
  949. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  950. jack_type = SND_JACK_UNSUPPORTED;
  951. #else
  952. jack_type = SND_JACK_HEADPHONE;
  953. #endif /* CONFIG_AUDIO_QGKI */
  954. break;
  955. }
  956. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_REM, false);
  957. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS, false);
  958. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_DETECTION_TYPE, 1);
  959. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_SCHMT_ISRC, 0);
  960. mbhc->extn_cable_hph_rem = false;
  961. wcd_mbhc_report_plug(mbhc, 0, jack_type);
  962. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  963. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 0);
  964. if (mbhc->mbhc_cb->clk_setup)
  965. mbhc->mbhc_cb->clk_setup(
  966. mbhc->component, false);
  967. }
  968. if (mbhc->mbhc_cfg->moisture_en ||
  969. mbhc->mbhc_cfg->moisture_duty_cycle_en) {
  970. if (mbhc->mbhc_cb->mbhc_moisture_polling_ctrl)
  971. mbhc->mbhc_cb->mbhc_moisture_polling_ctrl(mbhc,
  972. false);
  973. if (mbhc->mbhc_cb->mbhc_moisture_detect_en)
  974. mbhc->mbhc_cb->mbhc_moisture_detect_en(mbhc,
  975. false);
  976. }
  977. } else if (!detection_type) {
  978. /* Disable external voltage source to micbias if present */
  979. if (mbhc->mbhc_cb->enable_mb_source)
  980. mbhc->mbhc_cb->enable_mb_source(mbhc, false);
  981. /* Disable HW FSM */
  982. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  983. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  984. mbhc->extn_cable_hph_rem = false;
  985. }
  986. done:
  987. mbhc->in_swch_irq_handler = false;
  988. WCD_MBHC_RSC_UNLOCK(mbhc);
  989. pr_debug("%s: leave\n", __func__);
  990. }
  991. static irqreturn_t wcd_mbhc_mech_plug_detect_irq(int irq, void *data)
  992. {
  993. int r = IRQ_HANDLED;
  994. struct wcd_mbhc *mbhc = data;
  995. pr_debug("%s: enter\n", __func__);
  996. if (mbhc == NULL) {
  997. pr_err("%s: NULL irq data\n", __func__);
  998. return IRQ_NONE;
  999. }
  1000. if (unlikely((mbhc->mbhc_cb->lock_sleep(mbhc, true)) == false)) {
  1001. pr_warn("%s: failed to hold suspend\n", __func__);
  1002. r = IRQ_NONE;
  1003. } else {
  1004. /* Call handler */
  1005. wcd_mbhc_swch_irq_handler(mbhc);
  1006. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  1007. }
  1008. pr_debug("%s: leave %d\n", __func__, r);
  1009. return r;
  1010. }
  1011. int wcd_mbhc_get_button_mask(struct wcd_mbhc *mbhc)
  1012. {
  1013. int mask = 0;
  1014. int btn;
  1015. btn = mbhc->mbhc_cb->map_btn_code_to_num(mbhc->component);
  1016. switch (btn) {
  1017. case 0:
  1018. mask = SND_JACK_BTN_0;
  1019. break;
  1020. case 1:
  1021. mask = SND_JACK_BTN_1;
  1022. break;
  1023. case 2:
  1024. mask = SND_JACK_BTN_2;
  1025. break;
  1026. case 3:
  1027. mask = SND_JACK_BTN_3;
  1028. break;
  1029. case 4:
  1030. mask = SND_JACK_BTN_4;
  1031. break;
  1032. case 5:
  1033. mask = SND_JACK_BTN_5;
  1034. break;
  1035. default:
  1036. break;
  1037. }
  1038. return mask;
  1039. }
  1040. EXPORT_SYMBOL(wcd_mbhc_get_button_mask);
  1041. static void wcd_btn_lpress_fn(struct work_struct *work)
  1042. {
  1043. struct delayed_work *dwork;
  1044. struct wcd_mbhc *mbhc;
  1045. s16 btn_result = 0;
  1046. pr_debug("%s: Enter\n", __func__);
  1047. dwork = to_delayed_work(work);
  1048. mbhc = container_of(dwork, struct wcd_mbhc, mbhc_btn_dwork);
  1049. WCD_MBHC_REG_READ(WCD_MBHC_BTN_RESULT, btn_result);
  1050. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADSET) {
  1051. pr_debug("%s: Reporting long button press event, btn_result: %d\n",
  1052. __func__, btn_result);
  1053. wcd_mbhc_jack_report(mbhc, &mbhc->button_jack,
  1054. mbhc->buttons_pressed, mbhc->buttons_pressed);
  1055. }
  1056. pr_debug("%s: leave\n", __func__);
  1057. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  1058. }
  1059. static bool wcd_mbhc_fw_validate(const void *data, size_t size)
  1060. {
  1061. u32 cfg_offset;
  1062. struct wcd_mbhc_btn_detect_cfg *btn_cfg;
  1063. struct firmware_cal fw;
  1064. fw.data = (void *)data;
  1065. fw.size = size;
  1066. if (fw.size < WCD_MBHC_CAL_MIN_SIZE)
  1067. return false;
  1068. /*
  1069. * Previous check guarantees that there is enough fw data up
  1070. * to num_btn
  1071. */
  1072. btn_cfg = WCD_MBHC_CAL_BTN_DET_PTR(fw.data);
  1073. cfg_offset = (u32) ((void *) btn_cfg - (void *) fw.data);
  1074. if (fw.size < (cfg_offset + WCD_MBHC_CAL_BTN_SZ(btn_cfg)))
  1075. return false;
  1076. return true;
  1077. }
  1078. static irqreturn_t wcd_mbhc_btn_press_handler(int irq, void *data)
  1079. {
  1080. struct wcd_mbhc *mbhc = data;
  1081. int mask;
  1082. unsigned long msec_val;
  1083. pr_debug("%s: enter\n", __func__);
  1084. complete(&mbhc->btn_press_compl);
  1085. WCD_MBHC_RSC_LOCK(mbhc);
  1086. wcd_cancel_btn_work(mbhc);
  1087. if (wcd_swch_level_remove(mbhc)) {
  1088. pr_debug("%s: Switch level is low ", __func__);
  1089. goto done;
  1090. }
  1091. mbhc->is_btn_press = true;
  1092. msec_val = jiffies_to_msecs(jiffies - mbhc->jiffies_atreport);
  1093. pr_debug("%s: msec_val = %ld\n", __func__, msec_val);
  1094. if (msec_val < MBHC_BUTTON_PRESS_THRESHOLD_MIN) {
  1095. pr_debug("%s: Too short, ignore button press\n", __func__);
  1096. goto done;
  1097. }
  1098. /* If switch interrupt already kicked in, ignore button press */
  1099. if (mbhc->in_swch_irq_handler) {
  1100. pr_debug("%s: Swtich level changed, ignore button press\n",
  1101. __func__);
  1102. goto done;
  1103. }
  1104. mask = wcd_mbhc_get_button_mask(mbhc);
  1105. if (mask == SND_JACK_BTN_0)
  1106. mbhc->btn_press_intr = true;
  1107. if (mbhc->current_plug != MBHC_PLUG_TYPE_HEADSET) {
  1108. pr_debug("%s: Plug isn't headset, ignore button press\n",
  1109. __func__);
  1110. goto done;
  1111. }
  1112. mbhc->buttons_pressed |= mask;
  1113. mbhc->mbhc_cb->lock_sleep(mbhc, true);
  1114. if (schedule_delayed_work(&mbhc->mbhc_btn_dwork,
  1115. msecs_to_jiffies(400)) == 0) {
  1116. WARN(1, "Button pressed twice without release event\n");
  1117. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  1118. }
  1119. done:
  1120. pr_debug("%s: leave\n", __func__);
  1121. WCD_MBHC_RSC_UNLOCK(mbhc);
  1122. return IRQ_HANDLED;
  1123. }
  1124. static irqreturn_t wcd_mbhc_release_handler(int irq, void *data)
  1125. {
  1126. struct wcd_mbhc *mbhc = data;
  1127. int ret;
  1128. pr_debug("%s: enter\n", __func__);
  1129. WCD_MBHC_RSC_LOCK(mbhc);
  1130. if (wcd_swch_level_remove(mbhc)) {
  1131. pr_debug("%s: Switch level is low ", __func__);
  1132. goto exit;
  1133. }
  1134. if (mbhc->is_btn_press) {
  1135. mbhc->is_btn_press = false;
  1136. } else {
  1137. pr_debug("%s: This release is for fake btn press\n", __func__);
  1138. goto exit;
  1139. }
  1140. /*
  1141. * If current plug is headphone then there is no chance to
  1142. * get btn release interrupt, so connected cable should be
  1143. * headset not headphone.
  1144. * For ADC MBHC, ADC_COMPLETE interrupt will be generated
  1145. * in this case. So skip the check here.
  1146. */
  1147. if (mbhc->mbhc_detection_logic == WCD_DETECTION_LEGACY &&
  1148. mbhc->current_plug == MBHC_PLUG_TYPE_HEADPHONE) {
  1149. wcd_mbhc_find_plug_and_report(mbhc, MBHC_PLUG_TYPE_HEADSET);
  1150. goto exit;
  1151. }
  1152. if (mbhc->buttons_pressed & WCD_MBHC_JACK_BUTTON_MASK) {
  1153. ret = wcd_cancel_btn_work(mbhc);
  1154. if (ret == 0) {
  1155. pr_debug("%s: Reporting long button release event\n",
  1156. __func__);
  1157. wcd_mbhc_jack_report(mbhc, &mbhc->button_jack,
  1158. 0, mbhc->buttons_pressed);
  1159. } else {
  1160. if (mbhc->in_swch_irq_handler) {
  1161. pr_debug("%s: Switch irq kicked in, ignore\n",
  1162. __func__);
  1163. } else {
  1164. pr_debug("%s: Reporting btn press\n",
  1165. __func__);
  1166. wcd_mbhc_jack_report(mbhc,
  1167. &mbhc->button_jack,
  1168. mbhc->buttons_pressed,
  1169. mbhc->buttons_pressed);
  1170. pr_debug("%s: Reporting btn release\n",
  1171. __func__);
  1172. wcd_mbhc_jack_report(mbhc,
  1173. &mbhc->button_jack,
  1174. 0, mbhc->buttons_pressed);
  1175. }
  1176. }
  1177. mbhc->buttons_pressed &= ~WCD_MBHC_JACK_BUTTON_MASK;
  1178. }
  1179. exit:
  1180. pr_debug("%s: leave\n", __func__);
  1181. WCD_MBHC_RSC_UNLOCK(mbhc);
  1182. return IRQ_HANDLED;
  1183. }
  1184. static irqreturn_t wcd_mbhc_hphl_ocp_irq(int irq, void *data)
  1185. {
  1186. struct wcd_mbhc *mbhc = data;
  1187. int val;
  1188. pr_debug("%s: received HPHL OCP irq\n", __func__);
  1189. if (mbhc) {
  1190. if (mbhc->mbhc_cb->hph_register_recovery) {
  1191. if (mbhc->mbhc_cb->hph_register_recovery(mbhc)) {
  1192. WCD_MBHC_REG_READ(WCD_MBHC_HPHR_OCP_STATUS,
  1193. val);
  1194. if ((val != -EINVAL) && val)
  1195. mbhc->is_hph_ocp_pending = true;
  1196. goto done;
  1197. }
  1198. }
  1199. if (mbhc->hphlocp_cnt < OCP_ATTEMPT) {
  1200. mbhc->hphlocp_cnt++;
  1201. pr_debug("%s: retry, hphlocp_cnt: %d\n", __func__,
  1202. mbhc->hphlocp_cnt);
  1203. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 0);
  1204. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 1);
  1205. } else {
  1206. mbhc->mbhc_cb->irq_control(mbhc->component,
  1207. mbhc->intr_ids->hph_left_ocp,
  1208. false);
  1209. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  1210. mbhc->hph_status |= SND_JACK_OC_HPHL;
  1211. #endif /* CONFIG_AUDIO_QGKI */
  1212. wcd_mbhc_jack_report(mbhc, &mbhc->headset_jack,
  1213. mbhc->hph_status,
  1214. WCD_MBHC_JACK_MASK);
  1215. }
  1216. } else {
  1217. pr_err("%s: Bad wcd9xxx_spmi private data\n", __func__);
  1218. }
  1219. done:
  1220. return IRQ_HANDLED;
  1221. }
  1222. static irqreturn_t wcd_mbhc_hphr_ocp_irq(int irq, void *data)
  1223. {
  1224. struct wcd_mbhc *mbhc = data;
  1225. pr_debug("%s: received HPHR OCP irq\n", __func__);
  1226. if (!mbhc) {
  1227. pr_err("%s: Bad mbhc private data\n", __func__);
  1228. goto done;
  1229. }
  1230. if (mbhc->is_hph_ocp_pending) {
  1231. mbhc->is_hph_ocp_pending = false;
  1232. goto done;
  1233. }
  1234. if (mbhc->mbhc_cb->hph_register_recovery) {
  1235. if (mbhc->mbhc_cb->hph_register_recovery(mbhc))
  1236. /* register corruption, hence reset registers */
  1237. goto done;
  1238. }
  1239. if (mbhc->hphrocp_cnt < OCP_ATTEMPT) {
  1240. mbhc->hphrocp_cnt++;
  1241. pr_debug("%s: retry, hphrocp_cnt: %d\n", __func__,
  1242. mbhc->hphrocp_cnt);
  1243. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 0);
  1244. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 1);
  1245. } else {
  1246. mbhc->mbhc_cb->irq_control(mbhc->component,
  1247. mbhc->intr_ids->hph_right_ocp,
  1248. false);
  1249. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  1250. mbhc->hph_status |= SND_JACK_OC_HPHR;
  1251. #endif /* CONFIG_AUDIO_QGKI */
  1252. wcd_mbhc_jack_report(mbhc, &mbhc->headset_jack,
  1253. mbhc->hph_status, WCD_MBHC_JACK_MASK);
  1254. }
  1255. done:
  1256. return IRQ_HANDLED;
  1257. }
  1258. static int wcd_mbhc_initialise(struct wcd_mbhc *mbhc)
  1259. {
  1260. int ret = 0;
  1261. struct snd_soc_component *component = mbhc->component;
  1262. pr_debug("%s: enter\n", __func__);
  1263. WCD_MBHC_RSC_LOCK(mbhc);
  1264. /* enable HS detection */
  1265. if (mbhc->mbhc_cb->hph_pull_up_control_v2)
  1266. mbhc->mbhc_cb->hph_pull_up_control_v2(component,
  1267. HS_PULLUP_I_DEFAULT);
  1268. else if (mbhc->mbhc_cb->hph_pull_up_control)
  1269. mbhc->mbhc_cb->hph_pull_up_control(component, I_DEFAULT);
  1270. else
  1271. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HS_L_DET_PULL_UP_CTRL, 3);
  1272. /* Configure for moisture detection when duty cycle is not enabled.
  1273. * Otherwise disable moisture detection.
  1274. */
  1275. if (mbhc->mbhc_cfg->moisture_en && mbhc->mbhc_cb->mbhc_moisture_config
  1276. && !mbhc->mbhc_cfg->moisture_duty_cycle_en)
  1277. mbhc->mbhc_cb->mbhc_moisture_config(mbhc);
  1278. else if (mbhc->mbhc_cfg->moisture_duty_cycle_en &&
  1279. mbhc->mbhc_cb->mbhc_moisture_detect_en)
  1280. mbhc->mbhc_cb->mbhc_moisture_detect_en(mbhc, false);
  1281. /*
  1282. * For USB analog we need to override the switch configuration.
  1283. * Also, disable hph_l pull-up current source as HS_DET_L is driven
  1284. * by an external source
  1285. */
  1286. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  1287. if (mbhc->mbhc_cb->hph_pull_up_control_v2)
  1288. mbhc->mbhc_cb->hph_pull_up_control_v2(component,
  1289. HS_PULLUP_I_OFF);
  1290. else if (mbhc->mbhc_cb->hph_pull_up_control)
  1291. mbhc->mbhc_cb->hph_pull_up_control(component, I_OFF);
  1292. else
  1293. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HS_L_DET_PULL_UP_CTRL,
  1294. 0);
  1295. }
  1296. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHL_PLUG_TYPE, mbhc->hphl_swh);
  1297. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_GND_PLUG_TYPE, mbhc->gnd_swh);
  1298. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_SW_HPH_LP_100K_TO_GND, 1);
  1299. if (mbhc->mbhc_cfg->gnd_det_en && mbhc->mbhc_cb->mbhc_gnd_det_ctrl)
  1300. mbhc->mbhc_cb->mbhc_gnd_det_ctrl(component, true);
  1301. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HS_L_DET_PULL_UP_COMP_CTRL, 1);
  1302. /*
  1303. * Disable L_DET for USB-C analog audio to avoid spurious interrupts
  1304. * when a non-audio accessory is inserted. L_DET_EN sets to 1 when FSA
  1305. * I2C driver notifies that ANALOG_AUDIO_ADAPTER is inserted
  1306. */
  1307. if (mbhc->mbhc_cfg->enable_usbc_analog)
  1308. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 0);
  1309. else
  1310. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 1);
  1311. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  1312. /* Insertion debounce set to 48ms */
  1313. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_INSREM_DBNC, 4);
  1314. } else {
  1315. /* Insertion debounce set to 96ms */
  1316. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_INSREM_DBNC, 6);
  1317. }
  1318. /* Button Debounce set to 16ms */
  1319. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_DBNC, 2);
  1320. /* Enable micbias ramp */
  1321. if (mbhc->mbhc_cb->mbhc_micb_ramp_control)
  1322. mbhc->mbhc_cb->mbhc_micb_ramp_control(component, true);
  1323. /* enable bias */
  1324. mbhc->mbhc_cb->mbhc_bias(component, true);
  1325. /* enable MBHC clock */
  1326. if (mbhc->mbhc_cb->clk_setup) {
  1327. if (mbhc->mbhc_cfg->enable_usbc_analog)
  1328. mbhc->mbhc_cb->clk_setup(component, false);
  1329. else
  1330. mbhc->mbhc_cb->clk_setup(component, true);
  1331. }
  1332. /* program HS_VREF value */
  1333. wcd_program_hs_vref(mbhc);
  1334. wcd_program_btn_threshold(mbhc, false);
  1335. reinit_completion(&mbhc->btn_press_compl);
  1336. WCD_MBHC_RSC_UNLOCK(mbhc);
  1337. pr_debug("%s: leave\n", __func__);
  1338. return ret;
  1339. }
  1340. static void wcd_mbhc_fw_read(struct work_struct *work)
  1341. {
  1342. struct delayed_work *dwork;
  1343. struct wcd_mbhc *mbhc;
  1344. struct snd_soc_component *component;
  1345. const struct firmware *fw;
  1346. struct firmware_cal *fw_data = NULL;
  1347. int ret = -1, retry = 0;
  1348. bool use_default_cal = false;
  1349. dwork = to_delayed_work(work);
  1350. mbhc = container_of(dwork, struct wcd_mbhc, mbhc_firmware_dwork);
  1351. component = mbhc->component;
  1352. while (retry < FW_READ_ATTEMPTS) {
  1353. retry++;
  1354. pr_debug("%s:Attempt %d to request MBHC firmware\n",
  1355. __func__, retry);
  1356. if (mbhc->mbhc_cb->get_hwdep_fw_cal)
  1357. fw_data = mbhc->mbhc_cb->get_hwdep_fw_cal(mbhc,
  1358. WCD9XXX_MBHC_CAL);
  1359. if (!fw_data)
  1360. ret = request_firmware(&fw, "wcd9320/wcd9320_mbhc.bin",
  1361. component->dev);
  1362. /*
  1363. * if request_firmware and hwdep cal both fail then
  1364. * sleep for 4sec for the userspace to send data to kernel
  1365. * retry for few times before bailing out
  1366. */
  1367. if ((ret != 0) && !fw_data) {
  1368. usleep_range(FW_READ_TIMEOUT, FW_READ_TIMEOUT +
  1369. WCD_MBHC_USLEEP_RANGE_MARGIN_US);
  1370. } else {
  1371. pr_debug("%s: MBHC Firmware read successful\n",
  1372. __func__);
  1373. break;
  1374. }
  1375. }
  1376. if (!fw_data)
  1377. pr_debug("%s: using request_firmware\n", __func__);
  1378. else
  1379. pr_debug("%s: using hwdep cal\n", __func__);
  1380. if (ret != 0 && !fw_data) {
  1381. pr_err("%s: Cannot load MBHC firmware use default cal\n",
  1382. __func__);
  1383. use_default_cal = true;
  1384. }
  1385. if (!use_default_cal) {
  1386. const void *data;
  1387. size_t size;
  1388. if (fw_data) {
  1389. data = fw_data->data;
  1390. size = fw_data->size;
  1391. } else {
  1392. data = fw->data;
  1393. size = fw->size;
  1394. }
  1395. if (wcd_mbhc_fw_validate(data, size) == false) {
  1396. pr_err("%s: Invalid MBHC cal data size use default cal\n",
  1397. __func__);
  1398. if (!fw_data)
  1399. release_firmware(fw);
  1400. } else {
  1401. if (fw_data) {
  1402. mbhc->mbhc_cfg->calibration =
  1403. (void *)fw_data->data;
  1404. mbhc->mbhc_cal = fw_data;
  1405. } else {
  1406. mbhc->mbhc_cfg->calibration =
  1407. (void *)fw->data;
  1408. mbhc->mbhc_fw = fw;
  1409. }
  1410. }
  1411. }
  1412. (void) wcd_mbhc_initialise(mbhc);
  1413. }
  1414. static int wcd_mbhc_set_keycode(struct wcd_mbhc *mbhc)
  1415. {
  1416. enum snd_jack_types type;
  1417. int i, ret, result = 0;
  1418. int *btn_key_code;
  1419. btn_key_code = mbhc->mbhc_cfg->key_code;
  1420. for (i = 0 ; i < WCD_MBHC_KEYCODE_NUM ; i++) {
  1421. if (btn_key_code[i] != 0) {
  1422. switch (i) {
  1423. case 0:
  1424. type = SND_JACK_BTN_0;
  1425. break;
  1426. case 1:
  1427. type = SND_JACK_BTN_1;
  1428. break;
  1429. case 2:
  1430. type = SND_JACK_BTN_2;
  1431. break;
  1432. case 3:
  1433. type = SND_JACK_BTN_3;
  1434. break;
  1435. case 4:
  1436. type = SND_JACK_BTN_4;
  1437. break;
  1438. case 5:
  1439. type = SND_JACK_BTN_5;
  1440. break;
  1441. default:
  1442. WARN_ONCE(1, "Wrong button number:%d\n", i);
  1443. result = -1;
  1444. return result;
  1445. }
  1446. ret = snd_jack_set_key(mbhc->button_jack.jack,
  1447. type,
  1448. btn_key_code[i]);
  1449. if (ret) {
  1450. pr_err("%s: Failed to set code for %d\n",
  1451. __func__, btn_key_code[i]);
  1452. result = -1;
  1453. return result;
  1454. }
  1455. input_set_capability(
  1456. mbhc->button_jack.jack->input_dev,
  1457. EV_KEY, btn_key_code[i]);
  1458. pr_debug("%s: set btn%d key code:%d\n", __func__,
  1459. i, btn_key_code[i]);
  1460. }
  1461. }
  1462. if (btn_key_code[0])
  1463. mbhc->is_btn_already_regd = true;
  1464. return result;
  1465. }
  1466. #if IS_ENABLED(CONFIG_QCOM_FSA4480_I2C)
  1467. static int wcd_mbhc_usbc_ana_event_handler(struct notifier_block *nb,
  1468. unsigned long mode, void *ptr)
  1469. {
  1470. struct wcd_mbhc *mbhc = container_of(nb, struct wcd_mbhc, fsa_nb);
  1471. if (!mbhc)
  1472. return -EINVAL;
  1473. dev_dbg(mbhc->component->dev, "%s: mode = %lu\n", __func__, mode);
  1474. if (mode == TYPEC_ACCESSORY_AUDIO) {
  1475. if (mbhc->mbhc_cb->clk_setup)
  1476. mbhc->mbhc_cb->clk_setup(mbhc->component, true);
  1477. /* insertion detected, enable L_DET_EN */
  1478. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 1);
  1479. }
  1480. return 0;
  1481. }
  1482. #else
  1483. static int wcd_mbhc_usbc_ana_event_handler(struct notifier_block *nb,
  1484. unsigned long mode, void *ptr)
  1485. {
  1486. return 0;
  1487. }
  1488. #endif
  1489. int wcd_mbhc_start(struct wcd_mbhc *mbhc, struct wcd_mbhc_config *mbhc_cfg)
  1490. {
  1491. int rc = 0;
  1492. struct snd_soc_component *component;
  1493. struct snd_soc_card *card;
  1494. const char *usb_c_dt = "qcom,msm-mbhc-usbc-audio-supported";
  1495. if (!mbhc || !mbhc_cfg)
  1496. return -EINVAL;
  1497. component = mbhc->component;
  1498. card = component->card;
  1499. /* update the mbhc config */
  1500. mbhc->mbhc_cfg = mbhc_cfg;
  1501. dev_dbg(mbhc->component->dev, "%s: enter\n", __func__);
  1502. /* check if USB C analog is defined on device tree */
  1503. mbhc_cfg->enable_usbc_analog = 0;
  1504. if (of_find_property(card->dev->of_node, usb_c_dt, NULL)) {
  1505. rc = of_property_read_u32(card->dev->of_node, usb_c_dt,
  1506. &mbhc_cfg->enable_usbc_analog);
  1507. }
  1508. if (mbhc_cfg->enable_usbc_analog == 0 || rc != 0) {
  1509. dev_dbg(card->dev,
  1510. "%s: %s in dt node is missing or false\n",
  1511. __func__, usb_c_dt);
  1512. dev_dbg(card->dev,
  1513. "%s: skipping USB c analog configuration\n", __func__);
  1514. }
  1515. /* Parse fsa switch handle */
  1516. if (mbhc_cfg->enable_usbc_analog) {
  1517. dev_dbg(mbhc->component->dev, "%s: usbc analog enabled\n",
  1518. __func__);
  1519. mbhc->swap_thr = GND_MIC_USBC_SWAP_THRESHOLD;
  1520. mbhc->fsa_np = of_parse_phandle(card->dev->of_node,
  1521. "fsa4480-i2c-handle", 0);
  1522. if (!mbhc->fsa_np) {
  1523. dev_err(card->dev, "%s: fsa4480 i2c node not found\n",
  1524. __func__);
  1525. rc = -EINVAL;
  1526. goto err;
  1527. }
  1528. }
  1529. /* Set btn key code */
  1530. if ((!mbhc->is_btn_already_regd) && wcd_mbhc_set_keycode(mbhc))
  1531. pr_err("Set btn key code error!!!\n");
  1532. if (!mbhc->mbhc_cfg->read_fw_bin ||
  1533. (mbhc->mbhc_cfg->read_fw_bin && mbhc->mbhc_fw) ||
  1534. (mbhc->mbhc_cfg->read_fw_bin && mbhc->mbhc_cal)) {
  1535. rc = wcd_mbhc_initialise(mbhc);
  1536. if (rc) {
  1537. dev_err(card->dev, "%s: wcd mbhc initialize failed\n",
  1538. __func__);
  1539. goto err;
  1540. }
  1541. } else {
  1542. if (!mbhc->mbhc_fw || !mbhc->mbhc_cal)
  1543. schedule_delayed_work(&mbhc->mbhc_firmware_dwork,
  1544. usecs_to_jiffies(FW_READ_TIMEOUT));
  1545. else
  1546. pr_err("%s: Skipping to read mbhc fw, 0x%pK %pK\n",
  1547. __func__, mbhc->mbhc_fw, mbhc->mbhc_cal);
  1548. }
  1549. if (mbhc_cfg->enable_usbc_analog) {
  1550. mbhc->fsa_nb.notifier_call = wcd_mbhc_usbc_ana_event_handler;
  1551. mbhc->fsa_nb.priority = 0;
  1552. rc = fsa4480_reg_notifier(&mbhc->fsa_nb, mbhc->fsa_np);
  1553. }
  1554. return rc;
  1555. err:
  1556. dev_dbg(mbhc->component->dev, "%s: leave %d\n", __func__, rc);
  1557. return rc;
  1558. }
  1559. EXPORT_SYMBOL(wcd_mbhc_start);
  1560. void wcd_mbhc_stop(struct wcd_mbhc *mbhc)
  1561. {
  1562. pr_debug("%s: enter\n", __func__);
  1563. if (mbhc->current_plug != MBHC_PLUG_TYPE_NONE) {
  1564. if (mbhc->mbhc_cb && mbhc->mbhc_cb->skip_imped_detect)
  1565. mbhc->mbhc_cb->skip_imped_detect(mbhc->component);
  1566. }
  1567. mbhc->current_plug = MBHC_PLUG_TYPE_NONE;
  1568. mbhc->hph_status = 0;
  1569. if (mbhc->mbhc_cb && mbhc->mbhc_cb->irq_control) {
  1570. mbhc->mbhc_cb->irq_control(mbhc->component,
  1571. mbhc->intr_ids->hph_left_ocp,
  1572. false);
  1573. mbhc->mbhc_cb->irq_control(mbhc->component,
  1574. mbhc->intr_ids->hph_right_ocp,
  1575. false);
  1576. }
  1577. if (mbhc->mbhc_fw || mbhc->mbhc_cal) {
  1578. cancel_delayed_work_sync(&mbhc->mbhc_firmware_dwork);
  1579. if (!mbhc->mbhc_cal)
  1580. release_firmware(mbhc->mbhc_fw);
  1581. mbhc->mbhc_fw = NULL;
  1582. mbhc->mbhc_cal = NULL;
  1583. }
  1584. if (mbhc->mbhc_cfg->enable_usbc_analog)
  1585. fsa4480_unreg_notifier(&mbhc->fsa_nb, mbhc->fsa_np);
  1586. pr_debug("%s: leave\n", __func__);
  1587. }
  1588. EXPORT_SYMBOL(wcd_mbhc_stop);
  1589. /*
  1590. * wcd_mbhc_init : initialize MBHC internal structures.
  1591. *
  1592. * NOTE: mbhc->mbhc_cfg is not YET configure so shouldn't be used
  1593. */
  1594. int wcd_mbhc_init(struct wcd_mbhc *mbhc, struct snd_soc_component *component,
  1595. const struct wcd_mbhc_cb *mbhc_cb,
  1596. const struct wcd_mbhc_intr *mbhc_cdc_intr_ids,
  1597. struct wcd_mbhc_register *wcd_mbhc_regs,
  1598. bool impedance_det_en)
  1599. {
  1600. int ret = 0;
  1601. int hph_swh = 0;
  1602. int gnd_swh = 0;
  1603. u32 hph_moist_config[3];
  1604. struct snd_soc_card *card = component->card;
  1605. const char *hph_switch = "qcom,msm-mbhc-hphl-swh";
  1606. const char *gnd_switch = "qcom,msm-mbhc-gnd-swh";
  1607. const char *hs_thre = "qcom,msm-mbhc-hs-mic-max-threshold-mv";
  1608. const char *hph_thre = "qcom,msm-mbhc-hs-mic-min-threshold-mv";
  1609. pr_debug("%s: enter\n", __func__);
  1610. ret = of_property_read_u32(card->dev->of_node, hph_switch, &hph_swh);
  1611. if (ret) {
  1612. dev_err(card->dev,
  1613. "%s: missing %s in dt node\n", __func__, hph_switch);
  1614. goto err;
  1615. }
  1616. ret = of_property_read_u32(card->dev->of_node, gnd_switch, &gnd_swh);
  1617. if (ret) {
  1618. dev_err(card->dev,
  1619. "%s: missing %s in dt node\n", __func__, gnd_switch);
  1620. goto err;
  1621. }
  1622. ret = of_property_read_u32(card->dev->of_node, hs_thre,
  1623. &(mbhc->hs_thr));
  1624. if (ret)
  1625. dev_dbg(card->dev,
  1626. "%s: missing %s in dt node\n", __func__, hs_thre);
  1627. ret = of_property_read_u32(card->dev->of_node, hph_thre,
  1628. &(mbhc->hph_thr));
  1629. if (ret)
  1630. dev_dbg(card->dev,
  1631. "%s: missing %s in dt node\n", __func__, hph_thre);
  1632. ret = of_property_read_u32_array(card->dev->of_node,
  1633. "qcom,msm-mbhc-moist-cfg",
  1634. hph_moist_config, 3);
  1635. if (ret) {
  1636. dev_dbg(card->dev, "%s: no qcom,msm-mbhc-moist-cfg in DT\n",
  1637. __func__);
  1638. mbhc->moist_vref = V_45_MV;
  1639. mbhc->moist_iref = I_3P0_UA;
  1640. mbhc->moist_rref = R_24_KOHM;
  1641. } else {
  1642. mbhc->moist_vref = hph_moist_config[0];
  1643. mbhc->moist_iref = hph_moist_config[1];
  1644. mbhc->moist_rref = hph_moist_config[2];
  1645. }
  1646. mbhc->in_swch_irq_handler = false;
  1647. mbhc->current_plug = MBHC_PLUG_TYPE_NONE;
  1648. mbhc->is_btn_press = false;
  1649. mbhc->component = component;
  1650. mbhc->intr_ids = mbhc_cdc_intr_ids;
  1651. mbhc->impedance_detect = impedance_det_en;
  1652. mbhc->hphl_swh = hph_swh;
  1653. mbhc->gnd_swh = gnd_swh;
  1654. mbhc->micbias_enable = false;
  1655. mbhc->mbhc_cb = mbhc_cb;
  1656. mbhc->btn_press_intr = false;
  1657. mbhc->is_hs_recording = false;
  1658. mbhc->is_extn_cable = false;
  1659. mbhc->extn_cable_hph_rem = false;
  1660. mbhc->hph_type = WCD_MBHC_HPH_NONE;
  1661. mbhc->wcd_mbhc_regs = wcd_mbhc_regs;
  1662. mbhc->swap_thr = GND_MIC_SWAP_THRESHOLD;
  1663. if (mbhc->intr_ids == NULL) {
  1664. pr_err("%s: Interrupt mapping not provided\n", __func__);
  1665. return -EINVAL;
  1666. }
  1667. if (!mbhc->wcd_mbhc_regs) {
  1668. dev_err(component->dev, "%s: mbhc registers are not defined\n",
  1669. __func__);
  1670. return -EINVAL;
  1671. }
  1672. /* Check if IRQ and other required callbacks are defined or not */
  1673. if (!mbhc_cb || !mbhc_cb->request_irq || !mbhc_cb->irq_control ||
  1674. !mbhc_cb->free_irq || !mbhc_cb->map_btn_code_to_num ||
  1675. !mbhc_cb->lock_sleep || !mbhc_cb->mbhc_bias ||
  1676. !mbhc_cb->set_btn_thr) {
  1677. dev_err(component->dev, "%s: required mbhc callbacks are not defined\n",
  1678. __func__);
  1679. return -EINVAL;
  1680. }
  1681. /* No need to create new sound card jacks if is is already created */
  1682. if (mbhc->headset_jack.jack == NULL) {
  1683. ret = snd_soc_card_jack_new(component->card,
  1684. "Headset Jack", WCD_MBHC_JACK_MASK,
  1685. &mbhc->headset_jack, NULL, 0);
  1686. if (ret) {
  1687. pr_err("%s: Failed to create new jack\n", __func__);
  1688. return ret;
  1689. }
  1690. ret = snd_soc_card_jack_new(component->card,
  1691. "Button Jack",
  1692. WCD_MBHC_JACK_BUTTON_MASK,
  1693. &mbhc->button_jack, NULL, 0);
  1694. if (ret) {
  1695. pr_err("Failed to create new jack\n");
  1696. return ret;
  1697. }
  1698. ret = snd_jack_set_key(mbhc->button_jack.jack,
  1699. SND_JACK_BTN_0,
  1700. KEY_MEDIA);
  1701. if (ret) {
  1702. pr_err("%s: Failed to set code for btn-0\n",
  1703. __func__);
  1704. return ret;
  1705. }
  1706. INIT_DELAYED_WORK(&mbhc->mbhc_firmware_dwork,
  1707. wcd_mbhc_fw_read);
  1708. INIT_DELAYED_WORK(&mbhc->mbhc_btn_dwork, wcd_btn_lpress_fn);
  1709. }
  1710. mutex_init(&mbhc->hphl_pa_lock);
  1711. mutex_init(&mbhc->hphr_pa_lock);
  1712. init_completion(&mbhc->btn_press_compl);
  1713. /* Register event notifier */
  1714. mbhc->nblock.notifier_call = wcd_event_notify;
  1715. if (mbhc->mbhc_cb->register_notifier) {
  1716. ret = mbhc->mbhc_cb->register_notifier(mbhc, &mbhc->nblock,
  1717. true);
  1718. if (ret) {
  1719. pr_err("%s: Failed to register notifier %d\n",
  1720. __func__, ret);
  1721. return ret;
  1722. }
  1723. }
  1724. init_waitqueue_head(&mbhc->wait_btn_press);
  1725. mutex_init(&mbhc->codec_resource_lock);
  1726. switch (mbhc->mbhc_detection_logic) {
  1727. case WCD_DETECTION_LEGACY:
  1728. wcd_mbhc_legacy_init(mbhc);
  1729. break;
  1730. case WCD_DETECTION_ADC:
  1731. wcd_mbhc_adc_init(mbhc);
  1732. break;
  1733. default:
  1734. pr_err("%s: Unknown detection logic type %d\n",
  1735. __func__, mbhc->mbhc_detection_logic);
  1736. break;
  1737. }
  1738. if (!mbhc->mbhc_fn ||
  1739. !mbhc->mbhc_fn->wcd_mbhc_hs_ins_irq ||
  1740. !mbhc->mbhc_fn->wcd_mbhc_hs_rem_irq ||
  1741. !mbhc->mbhc_fn->wcd_mbhc_detect_plug_type ||
  1742. !mbhc->mbhc_fn->wcd_cancel_hs_detect_plug) {
  1743. pr_err("%s: mbhc function pointer is NULL\n", __func__);
  1744. goto err_mbhc_sw_irq;
  1745. }
  1746. ret = mbhc->mbhc_cb->request_irq(component,
  1747. mbhc->intr_ids->mbhc_sw_intr,
  1748. wcd_mbhc_mech_plug_detect_irq,
  1749. "mbhc sw intr", mbhc);
  1750. if (ret) {
  1751. pr_err("%s: Failed to request irq %d, ret = %d\n", __func__,
  1752. mbhc->intr_ids->mbhc_sw_intr, ret);
  1753. goto err_mbhc_sw_irq;
  1754. }
  1755. ret = mbhc->mbhc_cb->request_irq(component,
  1756. mbhc->intr_ids->mbhc_btn_press_intr,
  1757. wcd_mbhc_btn_press_handler,
  1758. "Button Press detect", mbhc);
  1759. if (ret) {
  1760. pr_err("%s: Failed to request irq %d\n", __func__,
  1761. mbhc->intr_ids->mbhc_btn_press_intr);
  1762. goto err_btn_press_irq;
  1763. }
  1764. ret = mbhc->mbhc_cb->request_irq(component,
  1765. mbhc->intr_ids->mbhc_btn_release_intr,
  1766. wcd_mbhc_release_handler,
  1767. "Button Release detect", mbhc);
  1768. if (ret) {
  1769. pr_err("%s: Failed to request irq %d\n", __func__,
  1770. mbhc->intr_ids->mbhc_btn_release_intr);
  1771. goto err_btn_release_irq;
  1772. }
  1773. ret = mbhc->mbhc_cb->request_irq(component,
  1774. mbhc->intr_ids->mbhc_hs_ins_intr,
  1775. mbhc->mbhc_fn->wcd_mbhc_hs_ins_irq,
  1776. "Elect Insert", mbhc);
  1777. if (ret) {
  1778. pr_err("%s: Failed to request irq %d\n", __func__,
  1779. mbhc->intr_ids->mbhc_hs_ins_intr);
  1780. goto err_mbhc_hs_ins_irq;
  1781. }
  1782. mbhc->mbhc_cb->irq_control(component, mbhc->intr_ids->mbhc_hs_ins_intr,
  1783. false);
  1784. clear_bit(WCD_MBHC_ELEC_HS_INS, &mbhc->intr_status);
  1785. ret = mbhc->mbhc_cb->request_irq(component,
  1786. mbhc->intr_ids->mbhc_hs_rem_intr,
  1787. mbhc->mbhc_fn->wcd_mbhc_hs_rem_irq,
  1788. "Elect Remove", mbhc);
  1789. if (ret) {
  1790. pr_err("%s: Failed to request irq %d\n", __func__,
  1791. mbhc->intr_ids->mbhc_hs_rem_intr);
  1792. goto err_mbhc_hs_rem_irq;
  1793. }
  1794. mbhc->mbhc_cb->irq_control(component, mbhc->intr_ids->mbhc_hs_rem_intr,
  1795. false);
  1796. clear_bit(WCD_MBHC_ELEC_HS_REM, &mbhc->intr_status);
  1797. ret = mbhc->mbhc_cb->request_irq(component,
  1798. mbhc->intr_ids->hph_left_ocp,
  1799. wcd_mbhc_hphl_ocp_irq, "HPH_L OCP detect",
  1800. mbhc);
  1801. if (ret) {
  1802. pr_err("%s: Failed to request irq %d\n", __func__,
  1803. mbhc->intr_ids->hph_left_ocp);
  1804. goto err_hphl_ocp_irq;
  1805. }
  1806. ret = mbhc->mbhc_cb->request_irq(component,
  1807. mbhc->intr_ids->hph_right_ocp,
  1808. wcd_mbhc_hphr_ocp_irq, "HPH_R OCP detect",
  1809. mbhc);
  1810. if (ret) {
  1811. pr_err("%s: Failed to request irq %d\n", __func__,
  1812. mbhc->intr_ids->hph_right_ocp);
  1813. goto err_hphr_ocp_irq;
  1814. }
  1815. mbhc->deinit_in_progress = false;
  1816. pr_debug("%s: leave ret %d\n", __func__, ret);
  1817. return ret;
  1818. err_hphr_ocp_irq:
  1819. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->hph_left_ocp, mbhc);
  1820. err_hphl_ocp_irq:
  1821. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_hs_rem_intr,
  1822. mbhc);
  1823. err_mbhc_hs_rem_irq:
  1824. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_hs_ins_intr,
  1825. mbhc);
  1826. err_mbhc_hs_ins_irq:
  1827. mbhc->mbhc_cb->free_irq(component,
  1828. mbhc->intr_ids->mbhc_btn_release_intr,
  1829. mbhc);
  1830. err_btn_release_irq:
  1831. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_btn_press_intr,
  1832. mbhc);
  1833. err_btn_press_irq:
  1834. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_sw_intr, mbhc);
  1835. err_mbhc_sw_irq:
  1836. if (mbhc->mbhc_cb->register_notifier)
  1837. mbhc->mbhc_cb->register_notifier(mbhc, &mbhc->nblock, false);
  1838. mutex_destroy(&mbhc->codec_resource_lock);
  1839. err:
  1840. pr_debug("%s: leave ret %d\n", __func__, ret);
  1841. return ret;
  1842. }
  1843. EXPORT_SYMBOL(wcd_mbhc_init);
  1844. void wcd_mbhc_deinit(struct wcd_mbhc *mbhc)
  1845. {
  1846. struct snd_soc_component *component = mbhc->component;
  1847. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_sw_intr, mbhc);
  1848. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_btn_press_intr,
  1849. mbhc);
  1850. mbhc->mbhc_cb->free_irq(component,
  1851. mbhc->intr_ids->mbhc_btn_release_intr,
  1852. mbhc);
  1853. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_hs_ins_intr,
  1854. mbhc);
  1855. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_hs_rem_intr,
  1856. mbhc);
  1857. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->hph_left_ocp, mbhc);
  1858. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->hph_right_ocp, mbhc);
  1859. if (mbhc->mbhc_cb && mbhc->mbhc_cb->register_notifier)
  1860. mbhc->mbhc_cb->register_notifier(mbhc, &mbhc->nblock, false);
  1861. if (mbhc->mbhc_fn->wcd_cancel_hs_detect_plug) {
  1862. WCD_MBHC_RSC_LOCK(mbhc);
  1863. mbhc->mbhc_fn->wcd_cancel_hs_detect_plug(mbhc,
  1864. &mbhc->correct_plug_swch);
  1865. WCD_MBHC_RSC_UNLOCK(mbhc);
  1866. }
  1867. mutex_destroy(&mbhc->codec_resource_lock);
  1868. mutex_destroy(&mbhc->hphl_pa_lock);
  1869. mutex_destroy(&mbhc->hphr_pa_lock);
  1870. }
  1871. EXPORT_SYMBOL(wcd_mbhc_deinit);
  1872. static int __init mbhc_init(void)
  1873. {
  1874. return 0;
  1875. }
  1876. static void __exit mbhc_exit(void)
  1877. {
  1878. }
  1879. module_init(mbhc_init);
  1880. module_exit(mbhc_exit);
  1881. MODULE_DESCRIPTION("wcd MBHC v2 module");
  1882. MODULE_LICENSE("GPL v2");