wcd-mbhc-v2.c 61 KB

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